To delay a function execution in JavaScript by 1 second, wrap a promise execution inside a function and wrap the Promise's resolve() in a setTimeout() as shown below. setTimeout() accepts time in milliseconds, so setTimeout(fn, 1000) tells JavaScript to call fn after 1 second.
jQuery delay() Method The delay() method sets a timer to delay the execution of the next item in the queue.
The delay() is an inbuilt method in jQuery which is used to set a timer to delay the execution of the next item in the queue. para1: It specifies the speed of the delay. para2: It is optional and specifies the name of the queue.
To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.
You can also just delay some operation this way:
setTimeout(function (){
// Something you want delayed.
}, 5000); // How long you want the delay to be, measured in milliseconds.
$.delay is used to delay animations in a queue, not halt execution.
Instead of using a while loop, you need to recursively call a method that performs the check every second using setTimeout
:
var check = function(){
if(condition){
// run when condition is met
}
else {
setTimeout(check, 1000); // check again in a second
}
}
check();
ES6 setTimeout
setTimeout(() => {
console.log("we waited 204586560000 ms to run this code, oh boy wowwoowee!");
}, 204586560000);
Edit: 204586560000 ms is the approximate time between the original question and this answer... assuming I calculated correctly.
If you are using ES6 features and you're in an async function, you can effectively halt the code execution for a certain time with this function:
const delay = millis => new Promise((resolve, reject) => {
setTimeout(_ => resolve(), millis)
});
This is how you use it:
await delay(5000);
It will stall for the requested amount of milliseconds, but only if you're in an async function. Example below:
const myFunction = async function() {
// first code block ...
await delay(5000);
// some more code, executed 5 seconds after the first code block finishes
}
jQuery's delay
function is meant to be used with effects and effect queues, see the delay
docs and the example therein:
$('#foo').slideUp(300).delay(800).fadeIn(400);
If you want to observe a variable for changes, you could do something like
(function() {
var observerInterval = setInterval(function() {
if (/* check for changes here */) {
clearInterval(observerInterval);
// do something here
}
}, 1000);
})();
JavaScript setTimeout
is a very good solution:
function funcx()
{
// your code here
// break out here if needed
setTimeout(funcx, 3000);
}
funcx();
The delay
function in jQuery is mostly used for delaying animations in a jQuery animation queue.
delay()
doesn't halt the flow of code then re-run it. There's no practical way to do that in JavaScript. Everything has to be done with functions which take callbacks such as setTimeout
which others have mentioned.
The purpose of jQuery's delay()
is to make an animation queue wait before executing. So for example $(element).delay(3000).fadeIn(250);
will make the element fade in after 3 seconds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With