利用javascript怎么做sleep功能?

heelin 2005-11-18 09:26:54
利用javascript怎么做sleep功能?
...全文
333 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2005-11-18
  • 打赏
  • 举报
回复
statement1;
wait (someDelay);
statement2;
you would stuff the code into functions
function statement1 () {
// your code here
}
function statement2 () {
// your code here
}
and call
statement1();
setTimeout('statement2()', someDelay);

If you wanted (pseudo code)
while (someCondition) {
statement1;
wait (someDelay)
}
you code
var tid;
function statement1 () {
// your code here
if (!condition)
clearInterval(tid);
}
tid = setInterval('statement1()', someDelay);

Note that both setInterval and setTimeout return a timer id you can use
to clear the scheduled execution e.g.
var tid = setTimeout('js code here', delayInMilliseconds);
...
clearTimeout(tid);
respectively
var tid = setInterval('js code here', delayInMilliseconds);
...
clearInterval(tid);



function pause(numberMillis) {
var now = new Date();
var exitTime = now.getTime() + numberMillis;
while (true) {
now = new Date();
if (now.getTime() > exitTime)
return;
}
}

87,919

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧