dt3674 2016-10-22 11:31
浏览 274
已采纳

如何在特定时间后自动运行Ajax请求?

I have an Ajax function (ajaxRequest) that I want to run automatically after some specific time (say 5 sec) without any user-prompt or warning . How can i do this? (I looked in the following links but found no solution )

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>
  • 写回答

6条回答 默认 最新

  • douan7529 2016-10-22 11:39
    关注

    Hello this is solution for the automatically send the ajax request

    Just put your function like this.

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错