Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

D3.js >= 3.1.10 transitions break when date.js is loaded #1302

Closed
baversjo opened this issue Jun 10, 2013 · 5 comments
Closed

D3.js >= 3.1.10 transitions break when date.js is loaded #1302

baversjo opened this issue Jun 10, 2013 · 5 comments

Comments

@baversjo
Copy link

I'm using http://www.datejs.com/ on the same page as d3, which break d3 transitions.

Date.js modifies the Date object in such way that's it's incompatible with the contract set by Javascript 1.5. In Date.js now() returns a Date object whereas the new, native method returns a numeric timestamp. From d3 3.1.10 Date.now is used in the internal "d3_timer_mark" function, which raised the issue. The now() method overridden by date.js is used by d3, and the symptom is that transitions fail silently in d3.

Although this should be resolved in Date.js (never will, not updated since release 2007), I do think something should be done in d3 as well. Date.js has a couple of million downloads on google code, so other people will cluelessly experience this issue. These are my two suggestions on how to move forward:

  1. Replace Date.now() with +new Date() in d3 and be done with it.
  2. Check the return value of Date.now(), and make sure it's a number.

Here's a example of how d3 behaves when date.js is loaded (fails silently):
http://bl.ocks.org/baversjo/ffe2d5081a787a4090eb
And when it's not (works!):
http://bl.ocks.org/mbostock/b4e46124258d1b75accb

@mbostock
Copy link
Member

As far as I can tell, Date.js does not redefine Date.now. This is easy to verify by pasting into your JavaScript console:

var s = document.createElement("script");
s.setAttribute("src", "//cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js");
document.head.appendChild(s);
Date.now();

Also, if you examine Date.now after loading Date.js, you’ll see:

function now() { [native code] }

Even if Date.js did redefine Date.now with a broken implementation, I have no desire to add workarounds for any and all libraries that break native methods. I recommend you don’t use them, or if you do, patch (or fork) the source to fix their broken implementations. Or, include your own workaround in the client as follows:

<script src="date.js"></script>
<script>Date.now = function() { return +new Date; };</script>

@baversjo
Copy link
Author

Mike,
Thanks for your response. It looks like the version on cloudflare is different from the direct download on their website. The cloudflare version does not override the Date.now method, so I guess I'll use that.

Are you sure that you don't want me to create a pull request which adds a check that throws an exception if Date.now returns the wrong value, just to let people know something is wrong and save them some pain?

@mbostock
Copy link
Member

Glad I could help. And although I appreciate your offer to help, I don’t want to burden D3 with workarounds for libraries that break standards; that would add complexity and bytes.

@nmccready
Copy link

This is related and thank you all for the work on this already. Not only does the cdn not have Date.now but the date of the build is totally different with no version bump.
jasondavies/d3-cloud#31

@nmccready
Copy link

If anyone cares the May 2008 build on the cdn is from the trunk. It is not labeled as a release.
https://code.google.com/p/datejs/source/browse/trunk/build/date-en-US.js

https://datejs.googlecode.com/svn/trunk/build/date-en-US.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants