简体   繁体   English

使用jQuery每隔xy秒执行一次PHP脚本

[英]Execute a PHP script every xy seconds with jQuery

I have a program that is calling a separate php script that is generating google map markers, that need to be placed onto google map. 我有一个程序正在调用一个单独的PHP脚本,该脚本会生成Google地图标记,需要将其放置到Google地图上。 The thing that I would like to do is to place that php include into jquery so that it would get executed every xy seconds. 我想做的是将php include放入jquery,以便每隔xy秒执行一次。

So basically, how to get this: 所以基本上,如何得到这个:

<?php include('buildmarkers.inc.php') ?>

to execute every xy seconds with jquery? 用jQuery执行每xy秒?

Do something like this 做这样的事情

setInterval(function(){ 
 $.ajax({url: "url of php script", success: function(result){
       Do whatver you want to do with output.
    }});
}, 3000);

Note 3000 is milliseconds 注意3000是毫秒

Use setTimeout to avoid overlapping issue with setInterval 使用setTimeout避免与setInterval重叠的问题
This method will execute ajax loop every 3 seconds after success or error callback successerror回调后,此方法将每3秒执行ajax循环

var ajaxLoop=function(delay){
  $.ajax({
         url: 'someURL',
         success: function(result){
           //success 
           setTimeout(ajaxLoop, delay);
        },
        error: function(err){
           //error occurred 
           setTimeout(ajaxLoop, delay);
        } 
  });
}

ajaxLoop(3000);  // delay = 3000

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

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