简体   繁体   中英

need to Execute the setInterval function without delay the first time

is there any way to show without delay for first time

so can get the value without delay for the first then refresh the value in background

 <script> function ajax() { var request = null; if (window.XMLHttpRequest) { request = new XMLHttpRequest(); } else if (window.ActiveXObject) { request = new ActiveXObject("Microsoft.XMLHTTP"); } if(request) { request.open("GET", "troops.php"); request.onreadystatechange = function () { document.getElementById("main").innerHTML = request.responseText; } } request.send(null); } setInterval("ajax()",250) </script> <div id="main"> </div>

just call ajax manually the first time (before setInterval )

also XMLHttpRequest is kind of legacy, fetch is the new thing

 <div id="main"></div> <script> const main = document.getElementById('main') const asText = r => r.text() const render = t => main.innerHTML = t function ajax() { fetch('https://httpbin.org/get').then(asText).then(render) } ajax() setInterval(ajax, 2000) </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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