Last active
August 3, 2017 21:47
-
-
Save magjac/d187319570dcdcf3beb39ae0aec75d81 to your computer and use it in GitHub Desktop.
D3 timer bug (?) 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<body> | |
<h1>Open the console and look for messages</h1> | |
<script src="//d3js.org/d3.v4.min.js"></script> | |
<script> | |
var period = 1000; | |
var i1 = d3.interval(f1, period); | |
var i2 = d3.interval(f2, period); | |
var n = 1; | |
var m = 100000000; | |
var t0 = t1 = t2 = Date.now(); | |
function f1(elapsed) { | |
var now = Date.now() | |
console.log('f1 now =', now, 'elapsed =', elapsed, 'real elapsed =', now - t0, 'elapsed error =', now - t0 - elapsed, 'interval error =', now - t1 - period); | |
console.time('f1 ' + n); | |
for (i = 0; i < n; i++) | |
; | |
console.timeEnd('f1 ' + n); | |
t1 = now; | |
} | |
function f2(elapsed) { | |
var now = Date.now() | |
console.log('f2 now =', now, 'elapsed =', elapsed, 'real elapsed =', now - t0, 'elapsed error =', now - t0 - elapsed, 'interval error =', now - t2 - period); | |
console.time('f2 ' + n); | |
for (i = 0; i < n; i++) | |
; | |
console.timeEnd('f2 ' + n) ; | |
t2 = now; | |
n = m; | |
m = m / 10; | |
if (n < 1) { | |
i1.stop(); | |
i2.stop(); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment