【问题标题】:How to Run Ajax request automatically after a specific time?如何在特定时间后自动运行 Ajax 请求?
【发布时间】:2016-10-22 11:31:52
【问题描述】:

我有一个 Ajax 函数 (ajaxRequest),我想在特定时间(比如 5 秒)后自动运行,而不需要任何用户提示或警告。我怎样才能做到这一点? (我查看了以下链接,但没有找到解决方案)

jQuery: How to make an AJAX request and continue without waiting for request to complete?

How do I run ajax request in background continuously?

How to run ajax call in background after some particulate time?

How to run execute page in background using ajax?

//My Code
//ajaxRequest Script..

<script type="text/javascript">
function ajaxRequest(map){

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", 'json_data.php', true);

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var myArr = JSON.parse(xmlhttp.responseText);
    setMarkers(map,myArr);
}
};
xmlhttp.send(null);
}
</script>

<script>
function myMap() {
var mapCanvas = document.getElementById("map");
var myCenter = new google.maps.LatLng(51.508742,-0.120850);
var lat=51.508742;
var lng=-0.140850;
var mapOptions = {center: myCenter, zoom: 5};
var map = new google.maps.Map(mapCanvas,mapOptions);

ajaxRequest(map);                                   //Called ajaxRequest

}
</script>

【问题讨论】:

  • 谷歌 setTimeout()

标签: javascript php ajax


【解决方案1】:

如果你想执行一次你的代码,你可以使用setTimeout(),如果你想每x秒执行一次你的代码,你可以使用setInterval()

setTimeout(function() {
    //The code you want to execute after 5 seconds
}, 5000);
//OR
setInterval(function() {
    //The code you want to execute every 5 seconds  
}, 5000);

【讨论】:

    【解决方案2】:

    您好,这是自动发送 ajax 请求的解决方案

    只要把你的函数像这样。

    function myMap() {
    var mapCanvas = document.getElementById("map");
    var myCenter = new google.maps.LatLng(51.508742,-0.120850);
    var lat=51.508742;
    var lng=-0.140850;
    var mapOptions = {center: myCenter, zoom: 5};
    var map = new google.maps.Map(mapCanvas,mapOptions);                     
    setInterval(function()
        {
          ajaxRequest(map);
        }, 5000);
    }
    

    【讨论】:

    • 这有帮助。感谢大家的帮助。
    【解决方案3】:

    如果您想每 5 秒执行一次 ajax 调用,请尝试以下操作:

    setInterval (function() { ajaxRequest(map); }, 5000);

    【讨论】:

      【解决方案4】:

      运行mymap()函数后,5sec中的ajax请求触发器。

      如果您需要在页面加载时运行 ajax,请按如下方式应用:

      <script>
      setTimeout(function() {ajaxRequest(map);},5000);
      </script>
      

      直接放在script

      <script type="text/javascript">
      function ajaxRequest(map){
      
      var xmlhttp = new XMLHttpRequest();
      xmlhttp.open("GET", 'json_data.php', true);
      
      xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          var myArr = JSON.parse(xmlhttp.responseText);
          setMarkers(map,myArr);
      }
      };
      xmlhttp.send(null);
      }
      </script>
      
      <script>
      function myMap() {
      var mapCanvas = document.getElementById("map");
      var myCenter = new google.maps.LatLng(51.508742,-0.120850);
      var lat=51.508742;
      var lng=-0.140850;
      var mapOptions = {center: myCenter, zoom: 5};
      var map = new google.maps.Map(mapCanvas,mapOptions);
      
      setTimeout(function() {ajaxRequest(map);},5000);    //Called ajaxRequest
      
      }
      </script>
      

      【讨论】:

        【解决方案5】:

        使用setTimeout(fn, time)在指定时间(ms)后调用一次函数或使用setIntreval(fn, time)在指定时间内重复调用函数

        【讨论】:

          【解决方案6】:

          试试这个...

          setTimeout(function() {
          //post your ajax code here
          }, 1000);

          更多信息请点击此处http://www.w3schools.com/jsref/met_win_setinterval.asp

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-12-21
            • 1970-01-01
            • 1970-01-01
            • 2011-01-23
            相关资源
            最近更新 更多