• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<script language="javascript">
4var last = new Date();    // The last time we sampled the timer
5var total_value = 0;      // The sum of the intervals measured
6var total_count = 0;      // The count of the intervals measured
7var last_interval = 1;
8function 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
44This page tests the frequency of setTimeout() in the browser.
45Javascript applications use setTimeout() as a mechanism to 'yield'
46to the browser so that the browser can repaint.  Most browsers
47implement a 15ms setTimeout() minimum.  Use this to page to measure
48setTimeout() lag and discover your browser's minimum interval.<P>
49
50<hr>
51
52Desired ms to delay: <input id="input" type="text" value="1"><P>
53
54Measured delay:<br>
55<ul>
56instance: <div id="output"></div>
57average: <div id="average"></div>
58</ul>
59
60</body>
61</html>
62