0

I'm building a polling sort of system to notify people if there is already someone in a particular page/field and would like to run a ping on that particular page/field to check if person is still sitting in there.

Given javascript:

function initialiseTrackPing(trackId) {
    var loopCounter = 1;
    //while (true) {
        setTimeout(function() {
            console.log(loopCounter++);
            iCommon.Ajax.unCloseTrack(trackId);
        }, 5000);
    //}
}

So I've commented the while loop out as it causes the whole browser to crash as the timeout seems not to cause the script to wait, so it loops to infinite as I suppose.

Inside the unCloseTrack(...) function there is an ajax call that will do some server-side work which I call asynchronous.

My Question:

How can I get the script not looping like crazy and just do so every 5000ms?

Thanks

KiwiJuicer
  • 1,952
  • 14
  • 28

3 Answers3

2

Oh thank you guys, the setInterval() was the probably most obvious solution for my little example but I have solved it in the meanwhile differently (working, see below):

I simply call the method first:...

iCommon.Methods.initialiseTrackPing(iCommon.Props.trackId);

...Which might do some other more stuff but basically calls the ajax method and passes on information:...

iCommon.Ajax.unCloseTrackPinger(trackId);

...And finally does the trick in calling itself always with a little timeout of in this case 5000ms in the ajax result:

function unCloseTrackPinger(trackId) {
    setTimeout(function() {
        console.log($.ajax({
            type: "GET",
            url: "ajax_switch.php",
            async: false,
            data: ({
                target: "tracks",
                type: "unCloseTrack",
                trackId: trackId
            }),
            success: function(result) {
                console.log(iCommon.Props.trackPingerCount++);
                unCloseTrackPinger(trackId);
            }
        }));
    }, 5000);
}

!

So my approach seems to do the job too!?

KiwiJuicer
  • 1,952
  • 14
  • 28
  • what is the difference between ``setTimeout`` and ``setInterval`` – Ehsan Sajjad May 21 '14 at 05:16
  • 1
    @EhsanSajjad [`setTimeout`](http://www.w3schools.com/jsref/met_win_settimeout.asp) executes the function after the specified milliseconds delay (5sec in this case) and is then done, [`setInteval`](http://www.w3schools.com/jsref/met_win_setinterval.asp) repeats executing the function every delay until explicitly stopped with [`clearInterval`](http://www.w3schools.com/jsref/met_win_clearinterval.asp). See also http://stackoverflow.com/questions/17486823/javascript-settimeout-vs-setinterval – Joe May 21 '14 at 18:35
  • @KiwiJuicer There is a subtle difference that may not matter in this instance. `setInterval` will trigger your function every 5 seconds, while recursively calling `setTimeout` will trigger your function 5 seconds after the previous call completes, *but only if it was successful*. If there is a momentary network hiccup or timeout that causes the `.ajax` call to fail once, the timeout will never be set again, until page reload or until something else calls `unCloseTrackPinger` again. – Joe May 21 '14 at 18:39
  • I would use `setTimeout` so that if a request takes longer than your interval you don't start stacking requests, but then put a call in the failure method too so that it retries in the case of a connectivity glitch. This has some useful techniques similar to KiwiJuicer's http://reallifejs.com/brainchunks/repeated-events-timeout-or-interval/ – Chris May 28 '16 at 15:40
1

use setInterval instead of setTimeout

Joe
  • 25,000
  • 3
  • 22
  • 44
1

use should use setInterval()

function initialiseTrackPing(trackId) {
    var loopCounter = 1;
    //while (true) {
        setInterval(function() {
            console.log(loopCounter++);
            iCommon.Ajax.unCloseTrack(trackId);
        }, 5000);
    //}
}
hutchbat
  • 806
  • 6
  • 14