10 examples of 'javascript sleep 2 seconds' in JavaScript

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

180function sleep(miliseconds) {
181 var currentTime = new Date().getTime();
182 while (currentTime + miliseconds >= new Date().getTime()) {
183 }
184}
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}
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}
15function sleep(seconds) {
16 let resolve;
17 const p = new Promise(r => resolve=r);
18 setTimeout(resolve, seconds*1000);
19 return p;
20}
82function sleep (milli) {
83 var date = new Date();
84 var curDate = null;
85 do { curDate = new Date(); }
86 while(curDate-date < milli);
87}
24function sleep(seconds) {
25 return new Promise((resolve) => setTimeout(resolve, 1000 * seconds));
26}
9function sleep(seconds) {
10 return new Promise(resolve => setTimeout(resolve, seconds * 1000));
11}
5export async function sleep(milliseconds: number): Promise<void> {
6 return new Promise<void>(resolve => setTimeout(resolve, milliseconds));
7}

Related snippets