[javascript]如何達成sleep的效果
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
使用方式:
假如要休眠1秒(1000milli seconds)
console.log(new Date());
console.log('Dude!');
sleep(1000);
console.log(new Date());
參考資料:
Javascript的Sleep()替代寫法 - 松露筆管麵
http://trufflepenne.blogspot.com/2011/09/javascriptsleep.html