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条)

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上