Why alert()
after setTimeout()
is not called as expected?
function run() {
setTimeout(\'\', 5000)
alert(\'Welcome!\')
}
run()
setTimeout('', 5000)
won't do anything. It won't delay the next line of code if that's what you're trying to do and by specifying an empty string for the first argument, you've not given it any code to run when the setTimeout()
does actually fire so it does nothing.
If you want the alert to fire in 5 seconds, then you do this:
setTimeout(function() {
alert("Welcome!");
}, 5000);
If you want the alert to pop up after the page has loaded, then you need to hook up to an event that signifies the page is loaded or just position your alert inside a <script>
tag right before the </body>
tag.
<html>
<body>
your HTML here
<script>
alert("Welcome!");
</script>
</body>
</html>