JavaScript (js) Sleep函數實作
JavaScript (js) Sleep函數實作
資料來源:http://trufflepenne.blogspot.com/2011/09/javascriptsleep.html
function sleep(milliseconds) { //http://trufflepenne.blogspot.com/2011/09/javascriptsleep.html var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { if ((new Date().getTime() - start) > milliseconds){ break; } } }
使用
//Sleep 1 秒 (1000 milliseconds):
console.log(new Date());
console.log(‘Dude!’);
sleep(1000);
console.log(new Date());