Curtis Crewe
Curtis Crewe

Reputation: 4336

javascript setTimeout not activating

When i'm using this code it's not activating the setTimeout on success return from jQuery

function waitForEvents(last_id){
$.ajax({
            type: "GET",
            url: "/functions/ajax.php?func=feed&old_msg_id="+last_id,

            async: true, /* If set to non-async, browser shows page as "Loading.."*/
            cache: false,
            timeout:50000, /* Timeout in ms */

            success: function(data){
                var json = jQuery.parseJSON(data);
                if(json !== 'null') {
                $.each(json.earnings, function (index, value) {
                     $('#feed').append(value);
                });
                var old_msg_id = json['last_id'];
                }
                alert("working");
                setTimeout('waitForEvents(last_id)',"1000");  
            },

            error: function (XMLHttpRequest, textStatus, errorThrown){
                alert("Error:" + textStatus + " (" + errorThrown + ")");
                setTimeout('waitForEvents(last_id)',"15000");       
            },
});
};

Any idea why because it's actually returning (data) so it's processing the response just not activating the settimeout again

Upvotes: 0

Views: 151

Answers (2)

Dan Saltmer
Dan Saltmer

Reputation: 2165

Your setTimeout method is not passing a function (apparently as a string is fine :/)

setTimeout(function() { waitForEvents(last_id); }, 15000);

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074475

The string you pass into setTimeout is evaluated at global scope. My guess is that either your function is not defined at global scope, or that there is no last_id value defined at global scope.

If your goal is to reuse the last_id argument that was passed to the function, thenchange your setTimeout call to:

setTimeout(function() {
    waitForEvents(last_id);
}, 1000); // Or 15000 for the other call

(Also note that the second argument should be a number, not a string.)

Here's an example of what I mean by the string being evaluated at global scope:

(function($) {

  $("#target").click(function() {
    setTimeout(foo, 500);
    setTimeout("bar()", 500);
    display("Timers started");
  });

  function foo() {
    display("foo called");
  }

  function bar() {
    display("bar called");
  }

  function display(msg) {
    $("<p>").html(String(msg)).appendTo(document.body);
  }

})(jQuery);

Live Example | Source

Assuming you have an element with the id "target" and you click it, after half a second you'll see "foo called" appear on the page, but you won't see "bar called". If you're using any modern browser, you'll see an error message in the JavaScript console saying that bar is undefined. That's because there is no global function called bar, there's only a function within the wrapper function called bar. So the string version fails.

Avoid passing strings to setTimeout whenever possible. It is always possible. (Apologies to the Dalai Lama.)

Upvotes: 0

Related Questions