Javascript等待秒

时间:2016-01-15 02:01:01

标签: javascript sleep

怎么能这样帮助我

删除这些按钮 用1秒

Remove Button

var i=0;
while(i<10) {
  var itemDivs = document.getElementsByClassName("remove");
  itemDivs[i].style.display = 'none';
  a++;
  sleep(1000); // is not working. I want wait 1 seconds and continue loop
}

1 个答案:

答案 0 :(得分:0)

您可能希望使用setTimeout函数。

这个函数有两个参数,时间(以毫秒为单位)和一个回调函数。

要实现睡眠类型方法,我建议将for循环更改为函数,然后使用setTimeout函数返回函数。

EX:

var i=0;
function foo() {`enter code here`
    i++;
    if(i<10) {
        itemDivs[a].style.display = 'none';
        a++;
        setTimeout(1000,foo); //Notice there are no parentheses after the function being passed into setTimeout.
    }
};