Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery sleep function? [duplicate]

Possible Duplicate:
Jquery: how to sleep or delay?

var main = $('#main_content');
main.append("<img id=\"throbber\" src='/pre_config/css/images/throbber.gif' alt='Loading. Please wait.' />");
sleep(3000);
$("#main_content").load("/pre_config/function.php?type="+type+"piz&count=1", successCallback );

I want to sleep for 3 seconds here

sleep is not defined
[Break on this error] sleep(3000);
func2
like image 926
Matt Elhotiby Avatar asked Sep 04 '10 18:09

Matt Elhotiby


2 Answers

Use a javascript setTimeout().

setTimeout(function() {
    $("#main_content").load("/pre_config/function.php?type="+type+"piz&count=1", successCallback );
}, 3000);

EDIT:

To use .delay(), you would need to add the delayed code to an animation queue.

$("#main_content")
.delay(3000)
.queue(function( n ) {
    $(this).load("/pre_config/function.php?type="+type+"piz&count=1", successCallback )
           .dequeue();
});
like image 112
user113716 Avatar answered Oct 04 '22 20:10

user113716


Keep in mind that JavaScript is not only single threaded, but it shares the same thread with the page rendering. Therefore there are no in-built blocking functions in JavaScript, because that would make the page UI unresponsive.

You should consider using timers instead, using setTimeout(), setInterval(), or jQuery's delay(). Check out @patrick's answer for a couple of examples.

like image 34
Daniel Vassallo Avatar answered Oct 04 '22 19:10

Daniel Vassallo