Java Sleep Function equivalent for Javascript

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 4601 Views - Last Post: 28 July 2010 - 03:23 PM

#1 oneal.michaels  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 25-June 10

Java Sleep Function equivalent for Javascript

Posted 23 July 2010 - 09:05 AM

Soo, the way that java's sleep function works is it waits for a specified amount of time and then continues execution, i know how to use the setTimeout function for javascript but this isn't what i am looking for, i need to know if there is a function like java's sleep function, or if there is a way to rig up a function using setTimeout to work like java's sleep function, thanks in advance for any help :)

Is This A Good Question/Topic? 0
  • +

Replies To: Java Sleep Function equivalent for Javascript

#2 oneal.michaels  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 25-June 10

Re: Java Sleep Function equivalent for Javascript

Posted 23 July 2010 - 09:25 AM

*headshakes*

well i found this dirty little function which does the job but it pretty much freezes up the whole browser for a period of time

function sleep(delay)
{
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}



and if the sleep is more than a few seconds then chances are that newer browsers will catch it and ask the user if they would like to terminate the script, so if i realllly had to pause then i would use it but i would rather not, and also im thinking that there probably isn't away to do this because java is a multi-threaded language as javascript is not...
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Java Sleep Function equivalent for Javascript

Posted 23 July 2010 - 04:29 PM

Wow, I don't want to visit your web pages. :P

Try breaking up your logic into two parts and use the timer.

e.g.
<html>
<head>
	<script type="application/javascript">
		function setResults(msg) { document.getElementById("results").innerHTML = msg; }

		function fire() { setResults("Fire!"); }

		function aim() {
			setResults("Aim.");
			setTimeout ( "fire()", 2000 );
		}
		
		function testDelay() {
			setResults("Ready!");
			setTimeout ( "aim()", 1000 );
		}
	</script>
</head>
<body>
	<div id="results"></div>
	<input type="button" onclick="testDelay();" id="bTest" value="Test Delay" />
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3389
  • View blog
  • Posts: 9,577
  • Joined: 08-June 10

Re: Java Sleep Function equivalent for Javascript

Posted 24 July 2010 - 04:07 AM

small improvement:
setTimeout ( aim, 1000 );

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Java Sleep Function equivalent for Javascript

Posted 24 July 2010 - 04:23 AM

I don't know that that's an improvement, just syntax preference. It's not like Javascript does a precompile or forward syntax checking.

However, if we want to play the better example game, my preferred way of writing the above would be this:
<script type="application/javascript">
	function setResults(msg) { document.getElementById("results").innerHTML = msg; }

	function testDelay() {
		setResults("Ready!");
		var delayCode = function() {
			setResults("Aim.");
			setTimeout ( 'setResults("Fire!");', 2000 );
		};
		setTimeout ( delayCode, 1000 );
	}
</script>



Note that the string literal usage allows for parameter passing. The syntax is much easier for a novice to follow. But first order functions are also fun.

Without the convenience eval, it looks something like:
setTimeout ( function() { setResults("Fire!"); }, 2000 );

This post has been edited by baavgai: 24 July 2010 - 04:23 AM

Was This Post Helpful? 0
  • +
  • -

#6 oneal.michaels  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 25-June 10

Re: Java Sleep Function equivalent for Javascript

Posted 24 July 2010 - 11:15 AM

haha, yea i wouldnt want to visit any of them either but im not using the function that i found instead i still have it broken down into two functions which isn't really what i wanted to do but it works just the same soooo... i guess it works... its just not as organized as if it were in java :P its been a while since i worked with java.. maybe i should start again :P
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Java Sleep Function equivalent for Javascript

Posted 24 July 2010 - 12:26 PM

View Postoneal.michaels, on 24 July 2010 - 12:15 PM, said:

its just not as organized as if it were in java :P its been a while since i worked with java.


Right, Java has about as much to do with Javascript as C++ or Visual Basic. As a duct typed language, it has more in common with maybe Python or Lua.

Saying "Java" and "Javascript" in the same breath is a sure sign you're well and truly lost. I'd recommend completely forgetting anything about Java and excising it from your mind. Look at Javascript as the distinct language that it is and you'll have a far easier time.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#8 oneal.michaels  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 25-June 10

Re: Java Sleep Function equivalent for Javascript

Posted 25 July 2010 - 09:17 AM

Yea i don't really mix up their functions or anything i was just trying to find and alternative to splitting up my functions as Java as well as PHP and maybe some other languages have a sleep function that is useful mostly for aesthetics because i want to show a loading page while my application is working, but at times when it only takes a half a second to load content from a server it just flashes my loading page up and then back down looking sloppy..
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Java Sleep Function equivalent for Javascript

Posted 25 July 2010 - 09:29 AM

What are you loading? Maybe there's another way to do this. Perhaps an event handler.
Was This Post Helpful? 0
  • +
  • -

#10 oneal.michaels  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 25-June 10

Re: Java Sleep Function equivalent for Javascript

Posted 25 July 2010 - 03:55 PM

I am using xmlhttprequest to get content from the last.fm audioscrobbler api, and i would like for there to be a 3-5 second pause in between loading just because i have to show a loading page in case of people with slow internet connections so they know my page is actually working, but in the case of fast internet connections it just flashes up my loading message on the screen and then takes it down immediately which looks sloppy...
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Java Sleep Function equivalent for Javascript

Posted 25 July 2010 - 06:21 PM

So you want to slow it down for everyone because of a few people on dialup??? :hungry:
If it were my site I'd have a hidden div that would have a "loading..." message and I'd bring that to the front and unhide it when loading and then hide it immediately when done. If you wanted to get really fancy you could have it transition in and out of view.
Was This Post Helpful? 0
  • +
  • -

#12 oneal.michaels  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 25-June 10

Re: Java Sleep Function equivalent for Javascript

Posted 26 July 2010 - 01:09 PM

Yea i had it fading in and out of view but when it loads faster it doesnt give it time to fade in and starts the fading out process too early, i tried using jquery to do it but it fades it all the way to black and there is no setting for the opacity that it fades to... so in short it would fade it out before it was done fading in, and then it would continue fading in and therefore get stuck...
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Java Sleep Function equivalent for Javascript

Posted 26 July 2010 - 05:05 PM

http://vision-media....itions-examples
From the above:
#example-4 {
  -webkit-transition-property: opacity;
  -webkit-transition-duration: 1s;
}
#example-4:hover {
  opacity: 0.1;
}


Was This Post Helpful? 0
  • +
  • -

#14 oneal.michaels  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 25-June 10

Re: Java Sleep Function equivalent for Javascript

Posted 27 July 2010 - 06:27 PM

That code is safari specific and i want my application to look as close as possible in each browser, i even sacrificed simplicity by using divs in the place of a listbox so i could style them equally in all browsers

EDIT: but those were some pretty cool effects, i wish they were supported on other browsers

This post has been edited by oneal.michaels: 27 July 2010 - 06:28 PM

Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Java Sleep Function equivalent for Javascript

Posted 27 July 2010 - 06:49 PM

They are supported by other browsers. Two examples: Opera and Chrome.
Firefox uses the Gecko engine, so you'd use:
-moz-transition-property: opacity;
-moz-transition-duration: 1s;

https://developer.mo...CSS_transitions
You should check this out:
http://html5readiness.com/
and then consider that people will not change their browsers until web developers give them a reason to. About 10 years ago lots of web sites had messages telling users that the site was "optimized" for IE. I suggest doing the same for HTML5 and CSS3. The sooner we get people used to the idea of needing a standards compliant browser the better off everyone will be.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2