10 examples of 'javascript await sleep' in JavaScript

Every line of 'javascript await sleep' 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
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}
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

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}
109function safeSleep(t) {
110 if (t == undefined) {
111 t = 200;
112 }
113
114 var start = Date.now();
115 while(rbm.running) {
116 sleep(200);
117 if (Date.now() - start >= t) {
118 break;
119 }
120 }
121}
10function sleep(ms) {
11 var start = new Date().getTime();
12 while(new Date().getTime() < start + ms);
13}
1297function sleep(ms) {
1298 return new Promise(resolve => setTimeout(resolve, ms));
1299}
1465export function sleep(ms) {
1466 const dt = new Date();
1467 dt.setTime(dt.getTime() + ms);
1468 while (new Date().getTime() < dt.getTime()) { }
1469}
15function sleep(seconds) {
16 let resolve;
17 const p = new Promise(r => resolve=r);
18 setTimeout(resolve, seconds*1000);
19 return p;
20}
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}
82function sleep (milli) {
83 var date = new Date();
84 var curDate = null;
85 do { curDate = new Date(); }
86 while(curDate-date < milli);
87}
43function Sleep(timeout) {
44 console.log('sleeping for', timeout)
45 return new Promise(function(resolve) {
46 setTimeout(resolve, timeout);
47 });
48}

Related snippets