I wanted to do something like this:
<p id="sec">5</p>
<script>
var i = 5;
while (i > 0){
setTimeout(i--,1000);
document.getElementById("sec").innerHTML = i;
}
if (i === 0){
window.location = "index.php";
}
</script>
And without a split second, it redirected me to index.php? Why does this happened? How to make it work?
setTimeouttakes a function to execute sometimes later, and returns immediately.setTimeout(alert("hi!"),1000);and you will understand what's happening really. PS: remove the redirect, just for testing purposes.setTimeout(function(){alert("timeout finished!")},5000);alert("hey, no waiting");See what happens, which alert comes first?