jaikl
jaikl

Reputation: 1091

How to make my function loop every x second in javascript

Can someone help me so that the function myFunction runs every X second. The function works perfectly if i only run it once, but when i begin using setInterval it is not run every x seconds. in the example below, it is sometimes executed after 5 sec and sometimes after 8 and so on. What should i change in my code? The api i´m using only grants 30 request per minute and i´m worried this function will exceed the limit as there are two getJSON.

setInterval(function () {
    myFunction();
}, 10000);

function myFunction() {

    $.getJSON("URL", function (data) {
        console.log(data);
        $.getJSON("URL", function (data2) {
            console.log(data2);

            variable1 = data.Departure;
            variable2 = data2.Departure;

            text = "";
            variable3 = variable1.concat(variable2);
            variable3.sort(function (a, b) {
                return new Date('2017/01/16 ' + a.time) - new Date('2017/01/16 ' + b.time);
            })

            console.log(variable3);
            for (var i = 0; i < variable3.length; i++) {
                text += variable3[i].name.substring(13, 19) + " departure " + variable3[i].time.substring(0, 5) + " from " + variable3[i].stop.substring(8) + "<br>";
            }
            document.getElementById("Textn").innerHTML = text;
            console.log(text);

        });
    });
}

myFunction();

Upvotes: 0

Views: 2239

Answers (3)

Afnan Ahmad
Afnan Ahmad

Reputation: 2542

Try This:

callfunction();
var callCount = 1;
var repeater = setInterval(function () {
  if (callCount <= 30) {
    callfunction();
    callCount += 1;
  } else {
    clearInterval(repeater);
  }
}, 1000);

function callfunction()
{
  console.log((new Date()).getSeconds());
}

Upvotes: 0

Satpal
Satpal

Reputation: 133403

Problem with the current implementation is that it uses asynchronous requests which may take more than 10 seconds(can't predict when the request is served).

The method setInterval will execute the myFunction irrespective of the previous requests were completed or not.

As a result next request will queue up to be executed. So you get it is sometimes executed after 5 sec and sometimes after 8 and so on.

You should use setTimeout() to recursively invoke the method instead of using setInterval

function myFunction() {
    $.getJSON("URL", function (data) {
        $.getJSON("URL", function (data2) {
            //Your existing code

            //Schedule it be executed after x * 1000 milliseconds
            setTimeout(myFunction, 10000);
        });
    });
}
myFunction();

Upvotes: 1

Supradeep
Supradeep

Reputation: 3266

Example setInterval implementation, This will run every 1 second. Your code should call the function every 10 seconds, Couldn't find any issue in that.

setInterval(myFunction, 1000); 

function myFunction(){
 console.log((new Date()).getSeconds());
};

myFunction();

Upvotes: 0

Related Questions