Not directly, but you can create a wrapper object to give that functionality. A rough implementation is like so:
function Timeout(fn, interval) {
var id = setTimeout(fn, interval);
this.cleared = false;
this.clear = function () {
this.cleared = true;
clearTimeout(id);
};
}
Then you can do something like:
var t = new Timeout(function () {
alert('this is a test');
}, 5000);
console.log(t.cleared); // false
t.clear();
console.log(t.cleared); // true
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…