test_setInterval_from_start.html (1458B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1252268 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test for Bug 1378394</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 11 </head> 12 <body> 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1378394">Mozilla Bug 1378394</a> 14 15 <script> 16 SimpleTest.waitForExplicitFinish(); 17 18 // Some of our platforms are quite slow. Use very large time values to 19 // try to avoid raciness. 20 const burnTimeMS = 5000; 21 const intervalTimeMS = 10000; 22 23 // The overall interval should include our callback "burn time". So we 24 // expect the delay to be the remaining time. 25 const expectedDelayMS = intervalTimeMS - burnTimeMS; 26 27 // Allow some margin for error because of slow test platforms. 28 const allowedMarginMS = burnTimeMS / 2; 29 30 let id; 31 let lastEndTime; 32 33 function interval() 34 { 35 let start = performance.now(); 36 37 if (lastEndTime !== undefined) { 38 let delta = start - lastEndTime; 39 ok(delta <= expectedDelayMS + allowedMarginMS, 40 'interval should not fire too late'); 41 ok(delta >= expectedDelayMS - allowedMarginMS, 42 'interval should not fire too early'); 43 clearInterval(id); 44 SimpleTest.finish(); 45 return; 46 } 47 48 while((performance.now() - start) < burnTimeMS); 49 50 lastEndTime = performance.now(); 51 } 52 53 id = setInterval(interval, intervalTimeMS); 54 </script> 55 56 </body> 57 </html>