javascript thread sleep
async function init() {
console.log(1);
await sleep(1000);
console.log(2);
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
// one liner
let sleep = ms => new Promise(resolve => setTimeout(resolve, ms));function pause(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
//Pause for 1000 milliseconds
pause(1000)//Javascipt is asyncronous so you can't pause/block code execution
//You can delay doing something with setTimeout
setTimeout(function(){
alert("Sup!");
}, 2000);//wait 2 secondssetTimeout(() => { console.log("World!"); }, 2000);
Also in JavaScript:
- transfer data from one component to another angular
- javascript switch case regex
- js set iframe src
- boton de copiar en html y js
- js datetime format
- js module.exports documentation comments
- reactnode prop-types
- JQuery UI Saving Sortable List
- javascript validate email
- javascript remove from array by index
- devtools failed to load sourcemap when debugging react native
- event loop in javascript
- js yesterday date
- face-api without projects
- eslint version check in react
- multiple image upload with preview and delete react js
- regular expression flags
- setstate react js
- how to insert a value into an array javascript
- name class and id referance in ajax
- javascript biggest number
- javascript sleep settimeout
- openssl error Subject does not start with '/'
- jsdoc for express routes