Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Please help on stopping the setTimeOut

Hi all, please help with getting a right function or method to stop the setTimeout function.

Say for example if I've triggered the setTimeout to happening in 3 secs and I have a button click event that I wish to be able to kill that timeout.

I've tried stopTimeout(), but it seems not doing anything.

Thanks in advance.

— asked March 14th 2010 by William Xue
  • iphone
  • javascript
  • mobile
0 Comments

14 Answers

  • This is still very very broken. Here are some replacement functions you can use as a workaround:

    function timeoutObject() { this.flag = true; }
    
    function mySetTimeout(callback, time) {
        var myCallback = callback;
        if (typeof callback == 'string') {
            myCallback = function() { eval(callback); };
        }
        var timerobj = new timeoutObject();
        setTimeout(function () { 
            if (timerobj.flag) { myCallback(); } 
        }, time);
        return timerobj;
    }
    
    function myClearTimeout(timer) {
        timer.flag = false;
    }
    

    Updated: added support for using a string to be eval'd instead of a function callback.

    — answered May 18th 2010 by Nick Wing
    permalink
    0 Comments
  • Can you post the small section of code where you start and stop the timer?

    — answered March 14th 2010 by Don Thorp
    permalink
    0 Comments
  • thanks, here is the code


    // Define Skip Intro click event

    btnIntro.addEventListener('click', function(e){
    fShowHome(e);
    Ti.API.info('MovieTimeout');
    stopTimeout(MovieTimeout);
    });

    // Define the movie time out

    var MovieTimeout = setTimeout(function(){fShowMovie();}, 3000);


    — answered March 14th 2010 by William Xue
    permalink
    0 Comments
  • Try clearTimeout instead of stopTimeout

    — answered March 14th 2010 by Don Thorp
    permalink
    0 Comments
  • Just tried with clearTimeout and the timeout has been triggered still.

    Does that means the clearTimeout() is not supported by Titanium? OR

    Any where I can get a correct javascript reference that works for Titanium apps?

    — answered March 14th 2010 by William Xue
    permalink
    0 Comments
  • clearTimeout(MovieTimeout) is supported in Titanium.

    — answered March 14th 2010 by Rob Edgell
    permalink
    0 Comments
  • I don't have a unit test to verify this yet, but it seems like calling clearTimeout actually causes the timeout function to fire.

    — answered March 15th 2010 by Alan McConnell
    permalink
    0 Comments
  • @Alan I've experienced the same issue, it somehow fires the timeout.

    Anyway I used a different approach to the same problem.

    Thanks again

    — answered March 15th 2010 by William Xue
    permalink
    0 Comments
  • I've noticed problems trying to get it to clear too, so now I just do it like this and no problems so far:

    var xhrCallCompleted = false;
    data = [];
    xhr.onload = function() {
        xhrCallCompleted = true;
        try {
            // do some stuff
        } catch(e) {
            // handle the error
        }
    };
    xhr.open("GET",url);
    xhr.send();
    setTimeout(function() {
        if (!xhrCallCompleted) {
            xhr.abort();
            // timeout occurred
        }
    }, interval_in_ms);
    
    — answered March 15th 2010 by Dan Giulvezan
    permalink
    1 Comment
    • When I use this code and set interval_in_ms to for example 10 I get this error: [ERROR] While executing Timer, received script error. 'Can't find variable: xhrCallCompleted.

      — commented May 22nd 2011 by James -
  • We need a reproducible test case to prove that clearTimeout doesn't work given that we've tested this a good bit.

    Here's the correct way to handle one-time timers:

    var timer = setTimeout(my_function,1000);
    clearTimeout(timer);
    

    Here's the correct way to handle repeating timers:

    var timer = setInterval(my_function,1000);
    clearInterval(timer);
    
    — answered March 16th 2010 by Jeff Haynie
    permalink
    0 Comments
  • @Alan: I have the same problem, the clearTimeout causes the timeout function to fire.

    — answered March 16th 2010 by Fabian Brussa
    permalink
    0 Comments
  • To add another voice to the chorus, I experienced this problem a few days ago. It was pretty easy to reproduce. I called clearTimeout and the function fired. I will try however to get something more formal together to prove it.

    BTW, a developer I'm working with filed a ticket on lighthouse:

    https://appcelerator.lighthouseapp.com/projects/32238/tickets/525-cleartimeout-triggers-the-call-instead-of-just-clearing

    — answered March 17th 2010 by Noah Davis
    permalink
    0 Comments
  • Here is a reproducible test case.

    Make it your app.js and run it.

    How to reproduce:

    1. Click "move"
    2. move a row. This sets a timeout to fire in 20 seconds.
    3. move another row within 20 seconds. The timeout will fire.

    I realize this could have been a simpler test, but I knew this would reproduce the problem, since this is how I ran into it in the first place. I will cross-post to lighthouse.

    — answered March 17th 2010 by Noah Davis
    permalink
    1 Comment
    • Hello,
      I used your way to reproduce the timer effect but it crashes my app… ?!?

      — commented June 12th 2011 by Eni Sinanaj
  • Here's a simpler test case: app.js

    — answered March 27th 2010 by Gunther Konig
    permalink
    1 Comment
    • it doesn't trigger the timeout… the alertDIalog doesn't come up

      — commented June 12th 2011 by Eni Sinanaj
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.