开发者

How to cause delay that work synchronic with the code -javascript

开发者 https://www.devze.com 2023-03-09 18:07 出处:网络
for(i=0;i<10;i++){ code setTimeout code } I want to prevent from the loop to proceed until tim开发者_运维百科eout is finish ,any idea??This repeats 10 times:
for(i=0;i<10;i++){

  code

  setTimeout

  code

}

I want to prevent from the loop to proceed until tim开发者_运维百科eout is finish ,any idea??


This repeats 10 times:

var j = 10;

setTimeout(someFunction, 1000);

function someFunction()
{
    if (j > 0)
        setTimeout(someFunction, 1000);
    j = j - 1;
}


var i = 0;

nextMove( );

function nextMove( ) {

    i++;

    if( i != 10 ) setTimeout(nextMove,3000);

}


If you've got Function.prototype.bind() available:

function operation() {
  if (!this) return;
  // do something
  setTimeout(operation.bind(--this), 1000);
}
setTimeout(operation.bind(10), 1000);


I believe there's no way to delay the execution of a block of code that follows an instruction that is being executed.

The only thing you can do is to wrap the code that need to be delayed around a function and use set time out just like the previous comments suggest.

0

精彩评论

暂无评论...
验证码 换一张
取 消