简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM