gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:14:47 25 4
gpt4 key购买 nike

我有一个 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() ,如果你想每 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);

关于javascript - 如何在特定时间后自动运行 Ajax 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40191609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com