I realize that this is an old question, but here's a plugin to address this issue that someone might find useful.
https://github.com/madbook/jquery.wait
lets you do this:
$('#myElement').addClass('load').wait(5000).addClass('done');
The reason why you should use .wait
instead of .delay
is because not all jquery functions are supported by .delay
and that .delay
only works with animation functions. For example delay does not support .addClass
and .removeClass
Or you can use this function instead.
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
sleep(5000);