Problem while using the while loop

2

I have input on a page where I type the zip code and can calculate the estimated value and time for delivery of a product for each region.

Clicking the calculate button will show a box loaded via Ajax with its information, the problem is that the platform has a small problem (bug) and shows 8 working days even for products that should be 4 working days . Home  My goal is to do a check for if the product has a price higher than $ 149.90 it performs a function to change the text from 8 business days to 4 business days. Home  The problem is that this element is not loaded right after clicking the button, it only appears when the Ajax request is completed (and I do not have access to the scripts that make this request). Home  So I face problems like the user's connection speed (which is variable from one to another), and the elements do not exist right after I click the button. Home  I tried to use the while loop, but I'm having problems, see the code:

window.addEventListener('DOMContentLoaded', function(){

if( document.querySelector('button[type="submit"]').textContent === 'OK' ){

    document.querySelector('button[type="submit"]').addEventListener('click', function(){

        var preco = /(\d+)(\,)(\d+)/.exec( document.getElementsByClassName('preco-promocional')[1].textContent );

        if( preco[1] > 149 ){
            while( typeof( document.getElementsByClassName('prazo')[1] ) === 'undefined' ){
                while( document.getElementsByClassName('prazo')[1].textContent === '8 dias úteis' ){
                    document.getElementsByClassName('prazo')[1].textContent = '4 dias úteis';
                }
            }
        }
    });
}

});

The logic would be to check while this element is undefined , and then, when it is no longer, I execute another loop to check if the text is 8 dias úteis and while it is, it tries to change to 4 dias úteis . Home Thanks for the attention

    
asked by anonymous 30.06.2014 / 21:20

1 answer

3

This will crash many browsers ... having a while to run this way will stop the browser. In cases where something is even the solution is a setInterval.

What you can do is to hide the parent element of this final element, and to show again only when you have changed the value.

And now test typeof preco[1] to see if it gives a number. If not your if will not work right.

Code hint:

window.addEventListener('DOMContentLoaded', function () {
    var botaoSubmit = document.querySelector('button[type="submit"]');
    if (botaoSubmit.textContent === 'OK') {
        botaoSubmit.addEventListener('click', function () {
            var preco = /(\d+)(\,)(\d+)/.exec(document.getElementsByClassName('preco-promocional')[1].textContent);
            if (preco[1] > 149) {
                var prazo = document.getElementsByClassName('prazo')[1];
                var espera = setInterval(function(){
                    if (!prazo) return;
                    prazo.textContent = '4 dias úteis';
                    clearInterval(espera);
                }, 100);
            }
        });
    }
});
    
30.06.2014 / 21:55