1

Any way to stop the code execution and then resume it? For example, next code will not run until a flag value is changed and then resume it.

Edit: I have a code that starts a loop of AJAX requests and I need to control (with a flag if its possible) thats loop when the user visits other tabs of the browser to stop it. Then, when the user returns to the application, loop would resume.

The problem would be solved with setInterval to AJAX requests and a flag to stop them. But I think that is more correct to do the loop of AJAX requests from the success ajax function, i.e. when the response is done, because I am using a MVC pattern and have the ajax function in a model and the called is done from de view. And then, the loop with setIntervalwould start in a wrong side

however, I am thinking that "setInterval" could be in the AJAX called function

vicenrele
  • 971
  • 3
  • 19
  • 37
  • 5
    Nope. Not unless you specifically designed it to manage its own state. Unless you're talking about debugging, where you can step through your code at your own rate. – Sampson Jan 10 '13 at 16:59
  • just so that we are clear, you want to pause the js execution, right? – Peeyush Kushwaha Jan 10 '13 at 17:00
  • 8
    It looks like you're looking for very bad solutions to a real problem. What's that problem ? – Denys Séguret Jan 10 '13 at 17:01
  • That's the very javascript way of doing things. It's called `event driven` design, patterns etc... The difference is you don't stop it but you custom start it – kidwon Jan 10 '13 at 17:06
  • Ok. I have a code that starts a loop of AJAX requests and I need to control (with a flag if its possible) thats loop when the user visits other tabs of the browser to stop it. Then, when the user returns to the application, loop would resume – vicenrele Jan 10 '13 at 17:07
  • 3
    @vicenrele: Sounds like you are looking to use the `Page Visiblity API`. Have a look at this SO issue: [is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active](http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active) You might also want to update your question in that case to make it more obvious that you are looking for a feature like this with some of your current code implementation you would like to update. As the uestion is right now it is not a fitting Q&A question I think. – Nope Jan 10 '13 at 17:13
  • Yep you can check it here, too http://stackoverflow.com/questions/1760250/how-to-tell-if-browser-tab-is-active – kidwon Jan 10 '13 at 17:16
  • Possible duplicate of http://stackoverflow.com/questions/936045/stop-an-executing-jquery-and-then-resume-where-it-left – Aditya Singh Jan 10 '13 at 17:18
  • Hi @François Wahl, but that functionality and I have implemented. I only need to stop the execution of some code – vicenrele Jan 10 '13 at 17:18
  • You could simply use setInterval to check regularly if you should make a new ajax request. – Denys Séguret Jan 10 '13 at 17:19
  • 1
    Well you'll have to start a new issue based on your very code. However if you want to stop something from executing you should not have executed it in the first place. That's probably a misconcept of code design – kidwon Jan 10 '13 at 17:22

1 Answers1

1

You can detect when the browser tab becomes inactive using any of many methods (I'm fond of using JQuery to set focus and blur events on the document, but there are other techniques), but I think the pattern you're looking for is the "closure that sets its own new timeout on completion" instead of setInterval.

var active = true;
function myloop()
{
    if (active)
    {
        $.ajax(/*...*/).done(function(j)
        {
            /* do stuff */ 
            window.setTimeout(myloop, interval);
        });
    }
}

In this scenario you'll need to set active = true and call myloop() again when you detect the tab or window becoming active again. When the tab becomes inactive, set active = false. The currently running AJAX request will complete safely; but further requests won't be made until you manually restart the loop.

Plynx
  • 11,341
  • 3
  • 32
  • 33