Reputation: 11
Is there any way to show without delay for the first time?
This is so I can get the value without delay for the first time and then refresh the value in the 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>
Upvotes: 1
Views: 34
Reputation: 37855
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>
Upvotes: 1