10 examples of 'javascript sleep in loop' in JavaScript

Every line of 'javascript sleep in loop' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
70async sleep() {
71 if (this.shuttingDown) {
72 return;
73 }
74 let timerPromise = new Promise(resolve => {
75 this.timeout = setTimeout(resolve, this.pollInterval);
76 });
77 log.trace(`[workloop %s] entering promise race`, this.label);
78 await Promise.race([this.waker.promise, timerPromise]);
79 log.trace(`[workloop] leaving promise race`, this.label);
80 if (this.timeout != null) {
81 clearTimeout(this.timeout);
82 }
83 this.internalWaker = undefined;
84}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

37function sleep(time, callback) {
38 var stop = new Date().getTime();
39 while(new Date().getTime() < stop + time);
40 if (callback && typeof callback === 'function') callback();
41}
194function sleep(milliseconds) {
195 var start = new Date().getTime();
196 for (var i = 0; i < 1e7; i++) {
197 if ((new Date().getTime() - start) > milliseconds){
198 break;
199 }
200 }
201}
1465export function sleep(ms) {
1466 const dt = new Date();
1467 dt.setTime(dt.getTime() + ms);
1468 while (new Date().getTime() < dt.getTime()) { }
1469}
10function sleep(ms) {
11 var start = new Date().getTime();
12 while(new Date().getTime() < start + ms);
13}
10function _sleep(t, callback) {
11 setTimeout(function() {
12 callback(null, null);
13 }, t);
14}
6function countdownTick(ms) {
7 if (ms < 0) return;
8 console.log('sleep', ms);
9 setTimeout(() => countdownTick(ms - 1000), 1000);
10}
180function sleep(miliseconds) {
181 var currentTime = new Date().getTime();
182 while (currentTime + miliseconds >= new Date().getTime()) {
183 }
184}
16export function loop(interval: number, callback: (...args: any[]) => any) {
17 const timer = setInterval(callback, interval)
18 return () => clearInterval(timer)
19}
82function sleep (milli) {
83 var date = new Date();
84 var curDate = null;
85 do { curDate = new Date(); }
86 while(curDate-date < milli);
87}

Related snippets