Jump to content

A function that waits a number of seconds before executing next line


kodejuice

Recommended Posts

Please how do i get around with this - I need a function that waits for like 2 seconds before moving on to the next line./*My Attempt*/function waitFor(secs){ return setTimeout(function(){ //waiting...}, (secs||1000));}alert("code started");waitFor(2000);alert("code stopped");

Edited by kodejuice
Link to comment
Share on other sites

Actually alert itself will stop the execution of your code, but if you try to stop your code with a loop the browser will be unhappy. Consider what this does...

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><style></style><script>window.onerror = function(m, u, l){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+l);return true;}</script><script>window.onload = init;function init(){var out = document.getElementById('out');out.innerHTML = "code started ";alert("code paused");out.innerHTML += "code000 ";alert("code paused");out.innerHTML += "code111 ";alert("code paused");out.innerHTML += "code222 ";setTimeout(function(){ out.innerHTML += "1 "; }, 1000);setTimeout(function(){ out.innerHTML += "2 "; }, 2000);setTimeout(function(){ out.innerHTML += "3 "; }, 3000);setTimeout(function(){ out.innerHTML += "4 "; }, 4000);setTimeout(function(){ out.innerHTML += "code ended"; }, 5000);setTimeout(function(){ alert("code 1"); }, 6000);setTimeout(function(){ alert("code 2"); }, 7000);setTimeout(function(){ alert("code 3"); }, 8000);setTimeout(function(){ alert("code 4"); }, 9000);setTimeout(function(){ alert("code ended"); }, 10000);}</script></head><body><div id="out"></div></body>    </html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...