trap
trap

Reputation: 2640

ajax call to REST service . Set Interval

From the self Host I want to get the data after every 5 seconds.

My Code in request.js:

    $.ajax({
        type: "GET",
        url: "http://localhost:8080/api/Data",
        success: function (data) {
            console.log(data);
        }
    });

What do I have to add?

Upvotes: 3

Views: 4343

Answers (1)

kakajan
kakajan

Reputation: 2707

Write function and set it to setInterval:

function checkData() {
    $.ajax({
        type: "GET",
        url: "http://localhost:8080/api/Data",
        success: function (data) {
            console.log(data);
        }
    });
}

setInterval(checkData, 5000);

you can use setTimeout if your ajax call gets longer time to get response:

function checkData() {
    $.ajax({
        type: "GET",
        url: "http://localhost:8080/api/Data",
        success: function (data) {
            console.log(data);
            setTimeout(checkData, 5000);
        }
    });
}

setTimeout(checkData, 5000);

Upvotes: 7

Related Questions