javascript sleep
Language/JAVASCRIPT 2011. 8. 31. 09:19DevCheater.com - javascript sleep
Have you ever needed the sleep command in JavaScript? Usually you can use the existing setTimeout(), clearTimeout() and setInterval() to accomplish what you want and in those cases you should use the native functions. But if you really need the sleep or wait statement lets go through the options and see what code might work the best.
Of course you can always skip to the conclusion.
What should the requirements of the sleep method be? I think power basic described it the most clearly as:
Pause the current thread of the application for a specified number of milliseconds (mSec), allowing other processes (or threads) to continue.
Power Basic sleep() - Win32 Sleep() - Java Thread.sleep()
The different ways in which we will make a javascript sleep function are:
- JavaScript sleep by loop - View Example
- JavaScript sleep by Java Applet - View Example
- JavaScript sleep by Flash - View Example
- JavaScript sleep by XMLHttp - View Example
JavaScript sleep by loop
The most basic way would be something like this:
1.
<script type=
"text/javascript"
>
2.
// bad implementation
3.
function
sleep(milliSeconds){
4.
var
startTime =
new
Date().getTime();
// get the current time
5.
while
(
new
Date().getTime() < startTime + milliSeconds);
// hog cpu
6.
}
7.
</script>
Analysis for javascript sleep loop
This implementation does block the running javascript but it also consumes all the CPU resources it can while sleeping and freezes all other javascript in the page (in all browsers tested).JavaScript sleep by Java Applet
One solution to the problem has ended up being in a Java Applet. Since you can communicate between javascript and java applets and that communication is blocking we can use Java's Thread.sleep() method (which when run uses almost no resources).
01.
<applet code=
"DevCheater.class"
name=
"devCheater"
id=
"devCheater"
mayscript=
"true"
height=
"1"
width=
"1"
></applet>
02.
03.
<script type=
"text/javascript"
>
04.
05.
function
sleep(milliSeconds){
06.
// runs Java Applets sleep method
07.
document.devCheater.sleep(milliSeconds);
08.
}
09.
10.
</script>
This version might look simpler then the last one but that is because some of the code is hidden away in the DevCheater.class Java file. On line 1 you can see an applet tag that utilizes my DevCheater.class Java class, this is what loads the Java program into the page so that it can be accessed by any javascript on it. Now this is where the bad news comes in. Not all browsers will allow other javascript to run while waiting for the Java sleep method to finish. Essentially sleeping all javascript on the page... See for your self JavaScript sleep by java applet example
Analysis for Java Applet
We do block the running javascript and no extra CPU resources are used while sleeping. This method does not freeze all other javascript in the page (except when using Chrome). Unfortunately it does require a java plug-in to be installed for their browser which limits its usefulness.JavaScript sleep by Flash
If we try using Java then why not adobe flash too. In this attempt I created a flash application that has a method called very unoriginally "flashSleep()". I now use javascript to call my flash method and pass in the number of milliseconds to sleep. But unfortunately this method also results in a freeze for most browsers I tested.
01.
<script type=
"text/javascript"
>
02.
03.
function
sleep(milliSeconds){
04.
// call sleep method in flash
05.
getFlashMovie(
"flashSleep"
).flashSleep(milliSeconds);
06.
}
07.
08.
function
getFlashMovie(movieName){
09.
// source: http://kb2.adobe.com/cps/156/tn_15683.html
10.
var
isIE = navigator.appName.indexOf(
"Microsoft"
) != -1;
11.
return
(isIE) ? window[movieName] : document[movieName];
12.
}
13.
14.
</script>
1.
<
object
classid
=
"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
codebase
=
"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"
width
=
"50"
height
=
"50"
id
=
"flashSleep"
align
=
"middle"
>
2.
<
param
name
=
"allowScriptAccess"
value
=
"sameDomain"
/>
3.
<
param
name
=
"allowFullScreen"
value
=
"false"
/>
4.
<
param
name
=
"movie"
value
=
"flashSleep.swf"
/>
5.
<
param
name
=
"quality"
value
=
"high"
/>
6.
<
param
name
=
"bgcolor"
value
=
"#ffffff"
/>
7.
<
embed
src
=
"flashSleep.swf"
quality
=
"high"
bgcolor
=
"#ffffff"
width
=
"50"
height
=
"50"
name
=
"flashSleep"
align
=
"middle"
allowScriptAccess
=
"sameDomain"
allowFullScreen
=
"false"
type
=
"application/x-shockwave-flash"
pluginspage
=
"http://www.macromedia.com/go/getflashplayer"
/>
8.
</
object
>
Analysis for Flash
Using adobe flash is no better then the rest when it comes to freezing. For that reason this method should not be used. javascript sleep by flash exampleJavaScript sleep by XMLHttp
Another way is to use server communication with XMLHttp.
01.
<script type=
"text/javascript"
>
02.
03.
function
sleep(milliSeconds){
04.
var
resource;
05.
var
response;
06.
if
(
typeof
ActiveXObject ==
'undefined'
){
07.
resource =
new
XMLHttpRequest();
08.
}
09.
else
{
10.
// IE
11.
resource =
new
ActiveXObject(
"Microsoft.XMLHTTP"
);
12.
}
13.
14.
try
{
15.
resource.open(
'GET'
,
'sleep.php?milliSeconds='
+ milliSeconds,
false
);
16.
resource.send(
null
);
17.
response = resource.responseText;
// JavaScript waits for response
18.
}
19.
catch
(e){
20.
alert(e);
21.
}
22.
23.
return
true
;
24.
}
25.
26.
</script>
01.
<?PHP
02.
$milliSeconds
=
intval
(
$_REQUEST
[
'milliSeconds'
]);
03.
if
(
$milliSeconds
> 60*1000){
04.
// limit server abuse
05.
$milliSeconds
= 10;
06.
}
07.
08.
usleep(
$milliSeconds
* 1000);
// note: usleep is in micro seconds not milli
09.
echo
"done"
;
10.
?>
Note: Any type of server software could be used in the place of Apache and PHP. IIS and ASP for example.
Analysis for XMLHttp
This does block the running javascript and no extra CPU resources are used on the user’s computer but it does add extra load on your server. As well this implementation does freeze all other javascript on the page in all browsers tested. The added server connections makes this method unacceptable for any use because it is bound to max out the servers allowed connections, essentially creating your own DDoS attacks on your server :-('Language > JAVASCRIPT' 카테고리의 다른 글
scrollHeight, clientHeight, offsetHeight (0) | 2011.09.28 |
---|---|
setTimeout (0) | 2011.08.31 |
body width, height [cross browsing] (0) | 2011.08.09 |
dynamic javaScript insertion (0) | 2011.08.09 |
자바스크립트 맵, javaScript Map (0) | 2011.08.09 |