Reputation: 4662
there is a built-in function (sleep()
) in php that delays execution of the current script for a specified number of seconds. it can be put anywhere and it will do the job.
here are its explanation:
http://www.w3schools.com/php/func_misc_sleep.asp
http://php.net/manual/en/function.sleep.php
my question: does a such function exist in javascript?
Upvotes: 1
Views: 95
Reputation: 10565
A piece of code would look like:
<html>
<head>
<script type="text/javascript">
function executeAfter(caseVal) {
switch(caseVal) {
case 0: // Code to execute before
alert("We are going for a sleep of 1 second");
setTimeout('executeAfter(1)', 1000);
break;
case 1: // Code to execute after sleep
alert("We are woke up");
break;
}
}
executeAfter(0);
</script>
</head>
</html>
Upvotes: 1
Reputation: 70552
JavaScript generally tries to be non-blocking. There is no sleep()
or wait()
or anything like that. Functions like setInterval()
or setTimeout()
can defer execution of a function to a later time, which is useful. However, the current script does not stop executing after those functions are called. It continues running until a separate built-in mechanism, whether it's a timer, a signal, or otherwise, executes the function that was specified in setTimeout()
.
Upvotes: 1
Reputation: 490163
Nope, and people's attempt with Date
and a while()
loop are such a bad idea I shouldn't have mentioned them. :P
You can, however, possibly achieve what you want with setInterval()
or setTimeout()
or a combination.
Upvotes: 3