I am new to Javascript, and recently got to know the use of setTimeout set to 0 millis. I am trying to implement it but not getting what is expected. As per my knowledge it has to wait until all events finished but it is not behaving expectely. Please tell me what I am missing or where I am wrong.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test HTML</title>
</head>
<body>
<script src="js/index.js"></script>
</body>
</html>
index.js
(function(){
setTimeout(function () {
document.write("<p>Event 0 Processed !!</p>");
},0);
setTimeout(function () {
document.write("<p>Event 1 Processed !!</p>");
},1000);
setTimeout(function () {
document.write("<p>Event 2 Processed !!</p>");
},2000);
setTimeout(function () {
document.write("<p>Event 3 Processed !!</p>");
},3000);
})();
output
Event 0 Processed !!
Event 1 Processed !!
Event 2 Processed !!
Event 3 Processed !!
I was expecting something like
Event 1 Processed !!
Event 2 Processed !!
Event 3 Processed !!
Event 0 Processed !!
Thanks :)