开发者

How to execute a JSON function every 2 seconds

This is the JSON function:

$.post("server.php", { "id": "studentid" },
   function(data){  
     if开发者_如何学编程(data.route)
     {
        window.location = data.location
     }

   }, 
   "json");

This redirects the user when a page gives a data.route . How can I make this function execute every 2 seconds so the page changes when that file changes.


window.setInterval(function(){$.post ... }, 2000)


You could use the window.setInterval method:

window.setInterval(function() {
    // sending AJAX POST requests every 2 seconds to the server
    // and if the server responds with data.route != null redirect
    $.post('server.php', { id: 'studentid' },
        function(data) {
            if(data.route) {
                window.location = data.location;
            }
        }, 
   'json');
}, 2000);


See setInterval(code,millis), e.g.:

setInterval("myUpdateFunction()", 2000);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜