简体   繁体   English

ajax调用REST服务。设置间隔

[英]ajax call to REST service . Set Interval

From the self Host I want to get the data after every 5 seconds. 从自我主机我想每5秒后获取数据。

My Code in request.js: 我在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 : 写函数并将其设置为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: 如果你的ajax调用获得响应的时间更长,你可以使用setTimeout

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

setTimeout(checkData, 5000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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