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.
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.
-
Can you post the small section of code where you start and stop the timer?
-
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);
-
Try
clearTimeout
instead ofstopTimeout
-
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?
-
clearTimeout(MovieTimeout) is supported in Titanium.
-
I don't have a unit test to verify this yet, but it seems like calling clearTimeout actually causes the timeout function to fire.
-
@Alan I've experienced the same issue, it somehow fires the timeout.
Anyway I used a different approach to the same problem.
Thanks again
-
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);
-
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);
-
@Alan: I have the same problem, the clearTimeout causes the timeout function to fire.
-
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
-
Here is a reproducible test case.
Make it your app.js and run it.
How to reproduce:
- Click "move"
- move a row. This sets a timeout to fire in 20 seconds.
- 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.
-
Here's a simpler test case: app.js