@Russ Cam is correct, setTimeout
is what you are looking for. The way you mentioned it in the question, though, makes me think that there might be some confusion about how it is used.
It will not block execution of the block it is in, rather, it will call its input function after a certain interval. As an illustration:
function illustration() {
// start out doing some setup code
alert("This part will run first");
// set up some code to be executed later, in 5 seconds (5000 milliseconds):
setTimeout(function () {
alert("This code will run last, after a 5 second delay")
}, 5000);
// This code will run after the timeout is set, but before its supplied function runs:
alert("This will be the second of 3 alert messages to display");
}
You will see three alert messages when you run this function, with a delay between the second and third:
- "This part will run first"
- "This will be the second of 3 alert messages to display"
- "This code will run last, after a 5 second delay"