http://www.devcheater.com


示例:

代码 复制 - 运行

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript sleep using server side sleep</title>
<meta name="keywords" content="javascript sleep, example, sleep method, sleep commnad, sleep thread, pause command, wait command, timer sleep" />
<meta name="description" content="A running example of a sleep function that uses XMLhttp to emulate a sleep statement for JavaScript." />
<meta name="author" content="Jamie Beck" />
<script type="text/javascript" src="../../jquery/js/jquery.js"></script>
<style type="text/css" media="all">
 /* classes for the blinking message to use */
 .show{
  display: block;
 }
 .hide{
  display: none;
 }
</style>
<script type="text/javascript">
 var toggleCount = 0; // used in browser test


 function sleep(milliSeconds){
  var resource;
  var response;
  if(typeof ActiveXObject == 'undefined'){
   resource = new XMLHttpRequest();
  }
  else{
   // IE
   resource = new ActiveXObject("Microsoft.XMLHTTP");
  }
 
  try{
   var nocache = new Date().getTime(); // parameters need to change or browser will use a cached version
   resource.open('GET', 'sleep.php?milliSeconds=' + milliSeconds + '&nochache=' + nocache, false);
   resource.send(null);
   response = resource.responseText;
  }
  catch(e){
   alert(e);
  }
  
  return true;
 }

 function verifySleep(milliSeconds){
  // wrapper function so add time tracking for demo
  var startTime = new Date().getTime(); // get the current time
  sleep(milliSeconds);
  alert('blocked for ' + ((new Date().getTime()) - startTime) + ' milli seconds');
 }


 // show if other javascript can continue running whle sleep is sleeping
 // i.e. the message element will keep blinking even when you are testing out the sleep command
 function toggleMessage(elementId){
  var targetElm = document.getElementById(elementId);

  if(targetElm.className == 'hide'){
   targetElm.className = 'show';
  }
  else{
   targetElm.className = 'hide';
  }
  toggleCount++;
 }
 // use a traditional timing method to make the message blink
 setInterval(function(){ toggleMessage('notFrozenMessage') }, 1000);


 // the following is a test that will record your browser and os version so
 // that we can collect stats on the different sleep methods
 function testBrowser(){
  alert('This test will most likely freeze your browser for 5 seconds.');
  var startTime = new Date().getTime(); // get the current time
  var startTickCount = toggleCount;
  
  sleep(5000);
  
  var runTime = (new Date().getTime()) - startTime; // get the run time
  var endTickCount = toggleCount;
  
  // save into our database
  $.post('testBrowser.php', {runtime: runTime, sleepLength: 5000, tickLength: 1000, tickCount: endTickCount - startTickCount, version: 'XMLhttp'}, function(data){
   alert(data); // display response
  });
  
 } // end function testBrowser()

</script>
</head>
<body>
<div style="float:right;"><a href="/" title="Home Page">Home</a> - <a href="/about.php" title="Who">About</a></div>
<p style="clear: both; float:right">
<strong title="Rmember to test all of the different implementsations.">Other Examples</strong><br />
<a href="loop.php" title="javascript sleep by loop example">Loop Example</a><br />
<a href="java.php" title="javascript sleep by java applet example">Java Example</a><br />
<a href="flash.php" title="javascript sleep by flash example">Flash Example</a><br />
<a href="XMLhttp.php" title="javascript sleep by xmlhttp example">XMLhttp Example</a>
</p>


<h1>javascript sleep example using server side sleep</h1>
<p>
Please view the source of this page so that you can see how it works!<br />
I find it helpful to use <a href="http://getfirebug.com/">FireBug</a> while playing around with new code.
</p>
 <p>I have added below a simple javascript blinking message as a test for your browser.<br />
 If when you use any of the sleep buttons on this page the blinking freezes... then using this <br />
 statment prevents any other javascript from running at the same time.<br />
 Not all browsers are the same.</p>

<div style="height:3em;">
 <div id="notFrozenMessage" class="hide"><strong>Other JavaScript can still run. (its a good thing :-)</strong></div>
</div>

<p>Note that when the javascript alert box containing the sleep duration pops up it will freeze the <br />
blinking message in most browsers. That is what we don't wan't to happen when the sleep command is executed.</p>


<p>
 <input type="button" name="btnTest" value="Test my Browser" onclick="testBrowser();" title="This will record your browser and os version as well as whether or not your browser freezes up while the sleep function is run." /> <span style="color:#FF0000">!! Please test your browser !!</span>
</p>

<p>
<input type="button" name="btnSleep" value="sleep(500)" onclick="verifySleep(500);" />
<input type="button" name="btnSleep" value="sleep(1000)" onclick="verifySleep(1000);" />
<input type="button" name="btnSleep" value="sleep(2000)" onclick="verifySleep(2000);" />
<input type="button" name="btnSleep" value="sleep(10000)" onclick="verifySleep(10000);" />
<input type="button" name="btnSleep" value="sleep(60000)" onclick="verifySleep(60000);" />
</p>

<p>
 DevCheater software is licenced under <a href="http://www.gnu.org/copyleft/lesser.html">LGPL 3</a> and free of charge.
</p>



<!-- just my stats collection under here  -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10272065-1");
pageTracker._setDomainName(".devcheater.com");
pageTracker._trackPageview();
} catch(err) {}</script></body>
</html>



上面代码主要归纳为:==================

代码 复制 - 运行

<script type="text/javascript">

function sleep(milliSeconds){
 var resource;
 var response;
 if(typeof ActiveXObject == 'undefined'){
  resource = new XMLHttpRequest();
 }
 else{
  // IE
  resource = new ActiveXObject("Microsoft.XMLHTTP");
 }

 try{
  resource.open('GET', 'sleep.php?milliSeconds=' + milliSeconds, false);
  resource.send(null);
  response = resource.responseText; // JavaScript waits for response
 }
 catch(e){
  alert(e);
 }
 
 return true;
}
 
</script>



Sleep.PHP =====================

代码 复制 - 运行

<?PHP
 $milliSeconds = intval($_REQUEST['milliSeconds']);
 if($milliSeconds > 60*1000){
  // limit server abuse
  $milliSeconds = 10;
 }
 
 usleep($milliSeconds * 1000); // note: usleep is in micro seconds not milli
 echo "done";
?>