Does anyone know how can I make a sleep in javascript before next line been read by the system?
example:
1 var chkResult = Validation();
2 /
1 var chkResult = Validation();
2 alert("wait ten seconds, then press ok");
3
4 document.getElementById('abc').innerHTML = chkResult;
The preceeding code delegates the sleep task to a parallel processor. This is a little messy because you have to use a DSL for this style of threading.
Try
setTimeout(function() { return true; }, 10000);
The first argument expects a function. This is from memory; I haven't tested it.
Edit: What Gumbo said... late here... not sure what I was thinking.
May I suggest this as a generic solution:
function sleep (ms, args, obj) {
/* Called at the top of a function to invoke a delay in processing that
function
Returns true if the function should be executed, and false if the sleep
timer is activated.
At the end of the sleep tiemout, the calling function is re-called by
calling it with "apply (obj, args || [])".
*/
var caller = sleep.caller;
if (caller.sleepTimer) {
/* if invoked from timeout function, delete timer and return false */
delete caller.sleepTimer;
return true;
}
/* Create timer and re-call caller at expiry */
caller.sleepTimer = window.setTimeout (function () {
caller.apply (obj || null, args || []);
},ms);
return false;
}
Examples of use:
function delayed () {
if (!sleep (5000)) return;
document.body.innerHTML += "<p>delayed processing</p>";
}
function delayed_args (a, b) {
if (!sleep (10000, arguments)) return;
document.body.innerHTML += "<p>args : (" + a + ", " + b + ")</p>";
}
function delayed_method_args (a,b) {
if (!sleep (20000, arguments, this)) return;
document.body.innerHTML += "<p>method_args " + this + " (" + a + ", " + b + ")</p>";
}
Tested on Opera and Firefox 3.0
Javascript has to share the thread with the browser, so the kind of pause you want would look very much like the browser was frozen for 10 seconds, were it possible. Functions such as "alert" and "prompt" do this. For this, they are evil, as a single alert in a loop can bring a whole browser to its knees, and the only way out is to force quit it.
The way around this problem is the event based paradigm. Javascript has first class functions, so they can be passed around as values for "callbacks" that you can assign to certain events.
setTimeout is one such event. Others have posted code, but in general, you'll want to just assign your functions to certain events, then get out of the way of the browser, so it can go about its browsery business. The browser will let you know when something has happened, and you can do something appropriate in response, quickly, then get out of the way again. In this way, the browser can maintain the appearance of being responsive.
if you want to know more, have a look at this question: How is GUI and Game Program Flow compared to Web programs
There is no sleep function in JS.
However, you can use setTimeout:
var chkResult = Validation();
setTimeout(() => { document.getElementById('abc').innerHTML = chkResult }, 10000);
A more modern way is to use promises and asynchronous functions:
const doThis = () => document.querySelectorAll('p')[0].innerHTML ='Do this';
const doThat = () => document.querySelectorAll('p')[1].innerHTML ='Do that';
const doAlsoThat = () => document.querySelectorAll('p')[2].innerHTML ='Do also that';
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
// using async function
(async () => {
doThis(); // executed at t=0s (approx)
await sleep(1000);
doThat(); // executed at t=1s (approx)
})();
// using promises
sleep(2000).then(doAlsoThat); // executed at t=2s (approx)
<p></p>
<p></p>
<p></p>
I Have the Hat has given the right hint. Use the setTimeout method to execute your forth line code after 10 seconds:
var chkResult = Validation();
var timeout = window.setTimeout(function() {
document.getElementById('abc').innerHTML = chkResult;
}, 10000);
Storing the timeout ID in a variable can be handy if you want to clear a timeout.