0

I.m currently following a tutorial about how to load content from a MYSQL db without reloading the page.

I just want to understand the use of setTimeout in this code. What's it for? I tried removing that part and ajax still works. Why would you need to delay a task, isn't ajax meant to be realtime update?

$(document).ready(function () {
    done(); 
});

function done() {
    setTimeout(function () {
        updates();
        done();
    }, 200);
}

function updates() {
    $.getJSON("update.php", function (data) {
        $("ul").empty();
        $.each(data.result, function () {
            $("ul").append("<li>ID: " + this['msg_id'] + "</li><br /><li>ID: " + this['msg'] + "</li><br />");
        });
    });
}
4
  • 2
    setTimeout reference: developer.mozilla.org/en/docs/Web/API/window.setTimeout Commented Aug 25, 2013 at 15:28
  • @JanDvorak delete that :) Commented Aug 25, 2013 at 15:30
  • @TusharGupta reworded to not refer to your comment ;-) Commented Aug 25, 2013 at 15:35
  • It's a recursive function that gets executed about five times every second. Without the timeout, it would be executed a million times every second and the browser would crash. Commented Aug 25, 2013 at 15:35

6 Answers 6

2

In that code, the setTimeout is being used to get updates from the server 5 times a second (which is probably too frequently). Without it, it only get the updates once (if updates() is ever called).

Sign up to request clarification or add additional context in comments.

Comments

0

The setTimeout appears in the done() function, and when the setTimeout executes (after 200ms) it calls itself recursively. Thus the updates() function will be called every 200ms for the life of the page.

If update.php is some kind of message stream, then this code requires the setTimeout to continuously poll for new messages and append them to a list.

Comments

0

settimeout and setTimeout

In your code setTimeout calls done function and updates() function after every 200 millisecond

function done() {
    setTimeout(function () {
        updates();
        done();
    }, 200);
}

like you if you remove setTimeout it still works

  function done() {
            updates();
            done();
    }

as it creates infinite loop of done function

but no delay which was caused by setTimeout

Comments

0

It's being used to delay recursion by 200 ms. If you remove the timeout, the loop will work just the same, but you'll probably run out of memory before too long.

A more interesting/instructive take on this example would be to pass done to updates as a callback and call it on success.

Comments

0

The function setTimeout takes a function and a number as parameter, it executes the function after the given number of milliseconds.

In your code it means: the function updates() is called every 200 milliseconds - 5 times a second.

Read more about it here: https://developer.mozilla.org/en/docs/Web/API/window.setTimeout

Comments

0

Your code uses setTimeout to poll regardless of the resource returning a result.

This version will only re-execute in case of success.

Of course if you want your code to poll always, regardless so it can recover from an error at the server, you should not change. But 200 MS is in any case far too often

$(function () {
  updates(); 
});


function updates() {
    $.getJSON("update.php", function (data) {
        $("ul").empty();
        $.each(data.result, function () {
            $("ul").append("<li>ID: " + this['msg_id'] + "</li>"+
                           "<li> " + this['msg'] + "</li>");
        });
        setTimeout(updates, 10000); // do it again in 10 seconds
    });
}

1 Comment

Whoever downvoted this and left no comment is more than unhelpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.