Set and clear timeouts in Javascript
Please, don’t use sleep! Use setTimeout, sleep function turn browser unresponsive, and it’s very annoying.
setTimeout(function() { alert('delayed alert') }, 2000);
If you want not call alert, for any reason, before 2s, you can use clearTimeout.
var timer = setTimeout(function() { alert('delayed alert') }, 2000);
// ...
// ...
if(foo) {
clearTimeout(timer);
}