mardi 8 septembre 2020

How to have XMLHttpRequest run once per request?

When this code receives a response from the server the code runs 2-3 times even if the totalPrice has not changed which means that the code within the "// CONDITIONS //" section fires multiple times.

Is there a way to only run the conditions from a single response, possibly narrowing down the response, or to run the condition only if the totalPrice has changed in value from the previous response?

                    var origOpen = XMLHttpRequest.prototype.open;
                        XMLHttpRequest.prototype.open = function () {
                            this.addEventListener("load", function () {
                                fetch("/script.js").then((response) => { 
                                    return response.json(); }).then((object) => {

                                    let totalPrice = object.total_price;
                                    console.log('total price', totalPrice);
                                
                                    // CONDITIONS //
                                    if (a < totalPrice){
                                    // do something // 
                                    }
                                    else (b > totalPrice){
                                    // do something else //
                                    }
                                   
                                    
                                })
                            }); origOpen.apply(this, arguments);
                        }

Aucun commentaire:

Enregistrer un commentaire