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);
精彩评论