locked
setTimeout in while doesn't work RRS feed

  • Question

  • User-1350042179 posted

    Hi

    I would like to run a setTimeout in a while, but I see that it doesn't work... I read that while doesn't recognize if the while statement finish.

    I would like to execute like this..

    while(q == 0)
    {
    
    setTimeout(function () {
    
    ajax.get("Integracion/ObtenerEstadoJob",
    	{
    		proceso: params.codigoIntegracion
    	}, function (data) {
    
    
         if (data === 'ERROR') {
    	q = 1;
         }
         else {
              if (data === 'EXITO') {
    
    	  q = 2;
              }
    	}
    });
    
    },5000);
    
    }
    
    
    

    Tuesday, May 28, 2019 4:13 AM

Answers

  • User665608656 posted

    Hi neoaguil,

    According to your description and the code you provided, the while loop does not wait for setTimeout to execute events, so it goes into a dead loop.

    To avoid this, you could better to call the Ajax operation in the method first, and then execute the setTimeout method to determine whether the current  'q' is 0. If it is 0, you could continue to call the method and cycle every five seconds until q is not equal to 0.

    For more details, you could refer to the following code:

               var q = 0;
                var myFunc01 = function () {
                    ajax.get("Integracion/ObtenerEstadoJob",
                        {
                            proceso: params.codigoIntegracion
                        }, function (data) {
                            if (data === 'ERROR') {
                                q = 1;
                            }
                            else {
                                if (data === 'EXITO') {
    
                                    q = 2;
                                }
                            }
                        });
                          
                        setTimeout(function () {
                            if (q == 0) {
                                myFunc01();
                            }
                        }, 5000);
                    }
    
                    myFunc01(); 

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 28, 2019 9:31 AM

All replies

  • User753101303 posted

    Hi,

    IMO the problem is that you are calling setTimeout in a while loop causing a lot of calls to be scheduled and so when the while loop ends those calls are still triggered. I would not use a loop. Instead I would call setTimeout to schedule the next call from the success callback depending on the situation.

    You could also call  https://www.w3schools.com/jsref/met_win_setinterval.asp one time only to call the same function every x second and clearInterval as needed to stop further calls.

    Tuesday, May 28, 2019 8:53 AM
  • User665608656 posted

    Hi neoaguil,

    According to your description and the code you provided, the while loop does not wait for setTimeout to execute events, so it goes into a dead loop.

    To avoid this, you could better to call the Ajax operation in the method first, and then execute the setTimeout method to determine whether the current  'q' is 0. If it is 0, you could continue to call the method and cycle every five seconds until q is not equal to 0.

    For more details, you could refer to the following code:

               var q = 0;
                var myFunc01 = function () {
                    ajax.get("Integracion/ObtenerEstadoJob",
                        {
                            proceso: params.codigoIntegracion
                        }, function (data) {
                            if (data === 'ERROR') {
                                q = 1;
                            }
                            else {
                                if (data === 'EXITO') {
    
                                    q = 2;
                                }
                            }
                        });
                          
                        setTimeout(function () {
                            if (q == 0) {
                                myFunc01();
                            }
                        }, 5000);
                    }
    
                    myFunc01(); 

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 28, 2019 9:31 AM
  • User-2054057000 posted

    The purpose of your code is to execute .setTimeout() in a loop i.e. many times. I suggest you to use .setInterval() method and remove the while loop.

    Now when you want to stop the .setInterval() method then you can use the .clearTimeout() method:

    var timer=setInterval(function(){ 
      //...
    }, 5000);
    
    clearTimeout(timer);



    Tuesday, May 28, 2019 1:14 PM