What does setTimeout return?
It can be an Object
, I tested it with node.js
:
var sto = setTimeout(
function(){console.log('ping');},
1000
);
console.log(sto);
The output is:
{ _idleTimeout: 1000,
_idlePrev:
{ '0': [Function: listOnTimeout],
_idleNext: [Circular],
_idlePrev: [Circular],
msecs: 1000 },
_idleNext:
{ '0': [Function: listOnTimeout],
_idleNext: [Circular],
_idlePrev: [Circular],
msecs: 1000 },
_idleStart: 2413359232,
_onTimeout: [Function],
_repeat: false,
domain:
{ domain: null,
_events: { error: [Function] },
_maxListeners: undefined,
members: [] } }
It's a handle (a unique identifier). When you create a timeout, the JavaScript runtime associates a handle with the timeout you created, and it can identify that timeout by the handle setTimeout()
returns. When you run clearTimeout()
, it will know what timeout you're talking about by looking at the unique handle you pass in.