我请鼠标点击下面的代码:为什么clearTimeout不适用于此代码?
clearTimeouts();
var inMotion = true, x = 0;
var shuffleTimer = setTimeout(function(){inMotion = false}, 3500);
var shuffleStart = setTimeout(oneShuffle, x);
function oneShuffle(){
x+=5;
if(inMotion === true){
console.log('Shuffling again');
//shuffle again
shuffleStart = setTimeout(oneShuffle, x);
} else {
//increment spins
spins++;
//reset spins if loadOrder been exhausted
spins === loadOrder.length ? spins = 0 : 0;
console.log(spins);
}
}
function clearTimeouts(){
console.log('Clearing timeouts')
clearTimeout(shuffleTimer);
clearTimeout(shuffleStart);
}
什么应该发生的事情是,如果我点击elment而inMotion
是true
两个超时应该重置和我spins
计数器不应该增加对以前的点击。然而,反而会发生什么事情,无论如何,前一次超时都会增加自旋。为什么?
你'的console.log()'调用在那里。你看到的日志输出的顺序是什么? – Pointy
使计时器成为全球? - 也许可以在代码的周围放置一个互斥区域,以便如果您清除超时时间太晚,则不会影响任何内容。 –