Home > front end >  Why he execution time is not allowed? Js loop execution time delay function
Why he execution time is not allowed? Js loop execution time delay function

Time:11-30

The function ff () {
$(' # 's1). The animate ({left:' 400 px, top: '30 pixels'});
}
The function ff2 () {
$(' # 's1). The animate ({left: "', top: "'});
}
Var ss=[3000500, 0600, 0600];
Var yy=[8000600, 0300, 0500];

//var j=0;
for(var i=0; iSetTimeout (function () {ff (); }, ss [I]);
//setTimeout (function () {ff (); }, ss [I] * I);
SetTimeout (function () {ff2 (); [I]}, yy);
The console. The log (ss [I]);
The console. The log (yy [I]);
}
From back-end 2 group, in the hope that in accordance with the relevant cycle subscript arrays to delay the execution code, now the problem that the delay time not
? Js loop execution time delay function, which a large coffee to help the cause

CodePudding user response:

Accuracy depends on the set of delay time to perform the function time although for loop execution speed quickly, but after all is not sync Settings;
The console. Time ()

The console. TimeEnd ()
Test the

CodePudding user response:

Is not the same as the results of each time? I add here right?

for(var i=0; iSetTimeout (function () {ff (); }, ss [I]);
//setTimeout (function () {ff (); }, ss [I] * I);
SetTimeout (function () {ff2 (); [I]}, yy);
the console. Time ();
The console. The log (ss [I]);
the console. TimeEnd ();

}

Default: 0.27294921875 ms

CodePudding user response:

Var I=0 to let I=0; Var level no block scope, you can understand cycle is synchronous setTimeout asynchronous execution cycle to finish the first value is the value of a variable in the end I just walked asynchronous

CodePudding user response:

The error is very small, now is the millisecond

CodePudding user response:

Or do you want a cumulative delay after delay last time?

CodePudding user response:

You should always think cumulative delay, such as yy output of 8000 milliseconds after waiting for output is 6000 milliseconds, then packaged into promise used to control, or async function, process control of a lot of ways

CodePudding user response:

 
//your two function
The function ff () {
The console. The log (' ff ')
}
The function ff2 () {
The console. The log (' ff2 ')
}

//how many seconds you two arrays, and it should be there
Const ss=[3000, 5000, 6000, 6000].
//,8,14,20 [3]
Const yy=[8000, 6000, 3000, 5000].
//,14,17,22 [8]


//wrap your function as a function of the promise
Const bePromise=fn=& gt; Timeout=& gt; The new Promise (resolve=& gt; SetTimeout ()=& gt; Resolve (fn ()), the timeout))

//to join the first function (curry)
Ff=bePromise (ff)
Ff2=bePromise (ff2)

//ff function of process management
Async function foo () {
For (the let I=0; iAwait the ff (ss [I])
}
}

//ff2 function of process management
Async function bar () {
For (the let I=0; i[I] await ff2 (yy)
}
}

//set a timer and see how many seconds of the output really
Let I=0
SetInterval ()=& gt; {
I++
The console. The log (I)
}, 1000);


//the function, began to output
Foo ()
Bar ()
  • Related