Can anyone do a javascript function that will not freeze the client ?
And dont show me
http://www.geekpedia.com/sample/JavaScriptSleep/sleep.htm
as that is a big BS. Simply use the below to see how big the BS is.
function Func1()
{
alert("Delayed 3 seconds");
}
function Func1Delay()
{
setTimeout("Func1()", 3000);
alert('i am first i am first ');
}
THE LINK with the above code is 5th on google "javascript sleep". The coder should be shot on sight.
Obviously you misunderstood the code. As for your browser that stops responding, I'm not sure what browser you are using, but it shouldn't pause the process for more than 3 seconds on IE6, IE7, Firefox (and any other Mozilla browser), Safari. If the process pauses for 3 seconds, that's the normal behaviour and that's the purpose of the code, as it was pointed out already the people that commented on the article, the code can be useful in some cases but cannot apply on others: http://www.geekpedia.com/Question55_How-do-I-make-a-JavaScript-function-wait-before-executing-(sleep-or-delay).html
"Javascript Sleep" does not match the function purpose. setTimeout doesnt stop the current thread, but applies a delay to the specified function so it does not "SLEEP" for the given time. Javascript Pseudo Sleep or Javascript Sleep Sim would be better. The function return cannot be delayed. For large js codes, breaking the code into functions and doing setTimeouts is at least inelegant if not BS. Anyway, there is no other alternative , so i better start faking sleep ...
(If anyone cares, I've been writing JavaScript professionally for 10 years now. Been there, done that, got the T-shirt, stained the T-shirt, washed the stain out.)
There just isn't any way to do what you want within the confines of JavaScript itself. Tight loops have undependable timing, so you can't use those. After a certain amount of time, the browser becomes worried that there's a problem in the page and offers to turn off the script, so you probably don't really want to do this anyway.
Once upon a time I simulated it with ActiveX a long time ago, and I suppose you could write a Java applet with a scriptable interface that would do the same thing--but it wouldn't keep the browser from worrying about why the page was taking so long to execute.
Using setTimeout() to create a delay does work, so long as you don't do something silly like execute other script in the function after you call setTimeout().
What I'm asking myself is why I'd want to force a delay in a web page anyway. If you're not trying to allow time for something to render in the page before launching some other new functionality, what is the point of an execution delay? While the delay occurs the page is inert (because of the way pages are threaded), so other than griefing, what would be the use?
10 years huh? Well it's not impossible. This will do exactly what you want: //pauses javascript on the page { }
Yes, that would, but the point of the discussion wasn't that it couldn't be done--it was that there's no synchronous sleep() function in Java/ECMAScript.
What you wrote works, of course, but according to MS documentation, showModalDialog isn't governed by any public standard, which I construe to mean that it's an IE-specific extension. One assumes that the OP wants a W3C-compliant solution to the problem--or at least one compatible with Firefox. (Javascript/ECMAScript itself recognizes no window object anyway--that's a DOM thing and outside the scope of pure scripting.) So, I can't accept it as a workable solution for a real-world project.
Yeah, 10 years. Done me homework, too.