kodejuice Posted December 17, 2014 Share Posted December 17, 2014 (edited) 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 December 17, 2014 by kodejuice Link to comment Share on other sites More sharing options...
justsomeguy Posted December 17, 2014 Share Posted December 17, 2014 Javascript can't do that, you don't want to just pause Javascript execution, it makes the browser hang. What you want to do is schedule a function to run after 5 seconds which will do the next task. 1 Link to comment Share on other sites More sharing options...
davej Posted December 18, 2014 Share Posted December 18, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now