Home | History | Annotate | Download | only in js
      1 <html>
      2 <head>
      3 <script language="javascript">
      4 var last = new Date();    // The last time we sampled the timer
      5 var total_value = 0;      // The sum of the intervals measured
      6 var total_count = 0;      // The count of the intervals measured
      7 var last_interval = 1;
      8 function fire() {
      9 
     10   var current = new Date();
     11   var ms = current - last;
     12 
     13   total_value += ms;
     14   total_count++;
     15 
     16   // Display the interval output.
     17   var output = document.getElementById('output');
     18   output.innerHTML = ms + "ms";
     19 
     20   // Display the average output.
     21   var average = document.getElementById('average');
     22   average.innerHTML = total_value / total_count + "ms";
     23 
     24   // Get the new interval from the input.
     25   var input = document.getElementById('input');
     26 
     27   // If the interval has changed, reset our averages.
     28   if (input.value != last_interval) {
     29     total_value = 0;
     30     total_count = 0;
     31   }
     32   last_interval = input.value;
     33 
     34   last = new Date();
     35   setTimeout(fire, last_interval);
     36 }
     37 </script>
     38 </head>
     39 
     40 <body onload='setTimeout("fire()", 1)'>
     41 
     42 <h1>Test JS setTimeout() speed</h1>
     43 
     44 This page tests the frequency of setTimeout() in the browser.
     45 Javascript applications use setTimeout() as a mechanism to 'yield'
     46 to the browser so that the browser can repaint.  Most browsers
     47 implement a 15ms setTimeout() minimum.  Use this to page to measure
     48 setTimeout() lag and discover your browser's minimum interval.<P>
     49 
     50 <hr>
     51 
     52 Desired ms to delay: <input id="input" type="text" value="1"><P>
     53 
     54 Measured delay:<br>
     55 <ul>
     56 instance: <div id="output"></div>
     57 average: <div id="average"></div>
     58 </ul>
     59 
     60 </body>
     61 </html>
     62