-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsleep.js
More file actions
32 lines (29 loc) · 1.13 KB
/
sleep.js
File metadata and controls
32 lines (29 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//
// Self adjustable WINDOWS Sleep function for nodejs sync
//
// If you use NODEJS for batch processes in windows, and you a tired on falling in the await/asycn nightmare, maybe this will help....
var Sleep = (function(i){
var iSleepAdjust = 50;
return function(i){
let stime = performance.now();
require('child_process').execSync("echo WSH.Sleep("+(i-iSleepAdjust)+"); > ~$timer.tmp & cscript //e:jscript /Nologo ~$timer.tmp & del ~$timer.tmp");
if (performance.now()-stime>=i) iSleepAdjust+=1;
else iSleepAdjust-=1;
}})();
/* OLD VERSION
// Sleep function for nodejs sync needs using different strategies
function Sleep(i)
{
if (i>=1000) {
if (i%1000) Sleep(i % 1000);
require('child_process').execSync("ping 127.0.0.1 -n "+Math.floor(i/1000+1)+" > nul");
}
else if (i>=200) {
require('child_process').execSync('powershell -command "Start-Sleep -Milliseconds '+Math.max(0, i-200)+'"');
}
else {
let stime = performance.now(); // started time
while (performance.now() - stime < i) require('child_process').execSync("ping 127.0.0.1 -n 1 > nul");
}
}
*/