Here's a link with some sample code.
/
I assumed, until I started testing, that the return value of a setTimeout()
would be some kind of browser object.
In fact it seems to be a simple integer. I also believed that assigning the return value of setTimeout
to a global variable would mean that there could only be one such object. But as you'll see if you click the "start timer" button more than one, there can be multiple timers running concurrently.
For instance
- I click the start button
- the script creates a timeout
- it is
timeout 1
, due to fire in five seconds - one second later, I click again and now there's a
timeout 2
- now I have
timeout 1
due to fire in four seconds andtimeout 2
in five seconds - I click the stop button and only
timeout 2
is cleared
The problem of user clicks creating multiple timers can be solved by always clearing the current timeout var before setting it. But I would really like to understand what Javascript is doing actually doing here.
Here's a link with some sample code.
http://jsfiddle/4djNt/2/
I assumed, until I started testing, that the return value of a setTimeout()
would be some kind of browser object.
In fact it seems to be a simple integer. I also believed that assigning the return value of setTimeout
to a global variable would mean that there could only be one such object. But as you'll see if you click the "start timer" button more than one, there can be multiple timers running concurrently.
For instance
- I click the start button
- the script creates a timeout
- it is
timeout 1
, due to fire in five seconds - one second later, I click again and now there's a
timeout 2
- now I have
timeout 1
due to fire in four seconds andtimeout 2
in five seconds - I click the stop button and only
timeout 2
is cleared
The problem of user clicks creating multiple timers can be solved by always clearing the current timeout var before setting it. But I would really like to understand what Javascript is doing actually doing here.
Share Improve this question edited Apr 10, 2020 at 8:12 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 26, 2012 at 2:11 AmbroseChapelAmbroseChapel 12.1k7 gold badges49 silver badges69 bronze badges 4- 1 "But I would really like to understand what Javascript is doing actually doing here." --- you have explained it in your "For instance" list – zerkms Commented Oct 26, 2012 at 2:13
-
1
The return value is sort of like a timer ID. To stop a particular timer, you run
clearTimeout(id)
. – Blender Commented Oct 26, 2012 at 2:13 - 1 From MDN "timeoutID is the numerical ID of the timeout, which can be used later with window.clearTimeout". When you assign it to the same global var, you are just overwriting the value – Phil Commented Oct 26, 2012 at 2:13
-
Imagine having an array
[...]
of all the timeouts. To access each one of them, JavaScript needs an ID, or the index of the timeout in the array. – JCOC611 Commented Oct 26, 2012 at 2:14
3 Answers
Reset to default 5setTimeout
and setInterval
return a number. The number is the id
of the timer. Active timers can be stopped using clearTimeout
and clearInterval
respectively.
If you lose the timer's id, you'd either have to guess it again, or wait for it to plete (assuming it's a timeout).
If the timer hasn't been cancelled in the number of milliseconds specified in the delay
parameter, it will execute the callback function in the global context.
The return value of setTimeout
can be passed to clearTimeout
to prevent a scheduled task from running. If the variable has been set and your button is pushed again, run clearTimeout
first, then setTimeout
assuming you want only the 2nd push to schedule.
The questioner asks only about timeout objects, which are returned in lieu of numbers in some environments, for instance timeoutObject
. For purposes of canceling the callback, passing the entire timeout object to clearTimeout(toObj)
does the trick.
{
called : bool
_idleTimeout : null
_idlePrev : null
_idleNext : null
_idleStart : null
_onTimeout : function (){}
_repeat : null
unref : function () {}
ref : function () {}
close : function () {}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744325331a4568621.html
评论列表(0条)