可以稱爲intTime
參數添加到您的function repeat
和功能在裏面可以調整下超時並調用復讀功能與新的超時。每次調用它將花費20毫秒的時間。但是,您通過將
var slowDown=20;
中的20
更改爲不同的數字來調整增量。
var slowDown=20;
setTimeout ("repeat",50);
function repeat(intTime){
//my code
if(!exit){
intTime=Math.floor (intTime)+slowDown;
setTimeout(repeat(intTime), intTime);
}
}
然後,您將需要爲退出創建另一個超時。
var time = Math.floor(Math.random() * (5000 - 3000 + 1)) + 3000;
var exit = false;
setTimeout ("stopSpinning",time);
function stopSpinning(){
exit = true;
}
所以整個事情應該是這個樣子
var slowDown=20;
var time = Math.floor(Math.random() * (5000 - 3000 + 1)) + 3000;
var exit = false;
setTimeout ("stopSpinning",time);
setTimeout ("repeat",50);
function repeat(intTime){
//my code
if(!exit){
intTime=Math.floor (intTime)+20;
setTimeout(repeat(intTime), intTime);
}
}
function stopSpinning(){
exit = true;
}
使用'Date.now()' –