开发者

How to execute a JSON function every 2 seconds

开发者 https://www.devze.com 2023-02-08 09:14 出处:网络
This is the JSON function: $.post(\"server.php\", { \"id\": \"studentid\" }, function(data){ if开发者_如何学编程(data.route)

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

精彩评论

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