user379888
user379888

Reputation:

setTimeout() does not delay

I am using the following piece of JavaScript in my website setTimeout() does not delay the function calling by 5 seconds.

function myFunction() {
    param = $('#search').val();
    //alert("I am an alert box!");
    if (param != "") {
        $("#status").show();
        //alert("Show ");
        var u = 'https://graph.facebook.com/search/?callback=&limit=5&q='+param;
        $("#data").empty();     
        alert("Wait for 5 sec?");
        setTimeout(getResults(u),50000);    // this line
        //getResults(u);
        //alert("When myFunction runs show more line 20");
        $("#more").show(); 
        }

    $("#more").click(function () { 
    $("#status").show();
    //alert("Show ");
    $("#more").hide();  
    pageTracker._trackPageview('/?q=/more');
    var u = nexturl;
    getResults(u);
  });
}

Upvotes: 1

Views: 913

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276286

You're calling the function itself instead of passing a function for setTimeout to call, try

 setTimeout(function(){
    getResults(u);
 },5000);//note time is in miliseconds, so 5 seconds is 5000 not 50000

Instead of setTimeout(getResults(u),50000);

I've created a small fiddle to illustrate how this works.

Here is the MDN article about setTimeout. It has nice examples about how it works.

Upvotes: 3

Related Questions