4 ms·
Stop writing milliseconds and hating JavaScript's setTimeout & setInterval
- avk 15y agoAuthor here. Very open to feedback & criticism. Thanks!
- nasht 15y agoI could see this being really useful if it could message a javascript event bus. Maybe use it for deciding when to synch browser data with server data or to modulate the messaging between different users browsers. The statelessness of http creates many timing related problems. On a side note: when is one of these big companies going to release a really advanced "jquery for webkit" that forgets about the browser incompatibilities and focuses on building a virtual operating system in the browser? Google, Apple, Netflix? Anyone?
- fuckingromans 15y agouse a javascript message train instead!
- avk 15y agoThanks for the idea but that's definitely much more complex than what I had in mind: just encouraging people to use more readable JS timers. Feel free to build on top of my code though!
- stephenr 15y agoI'm sorry but if you can't immediately realize upon reading that 5 * 60 * 1000 is five minutes, you probebly shouldn't be trying to use window.setTimeout and definitely not window.setInterval. Not every single thing needs to be wrapped in a jQuery "helper". Doing that just makes the developer using it more reliant on jQuery and less experienced/knowledgeable about straight JavaScript.
- johnswamps 15y agoClearly any semi-decent programmer can mentally translate 5 * 60 * 1000 into 5 minutes. So what? This is nonetheless a more intuitive and less error prone way to write times. This is about making readers of your code spend less cognitive effort figuring out what your code does, decreasing the chances of bugs (oops, I missed a 0 or screwed up a conversion factor), and making your code easier to modify. You seem to think this is a plugin for programmers too dumb to use milliseconds; it's not. Stop making excuses for bad code. That said, sure, this isn't exactly a huge issue that's plaguing the javascript community. I probably won't use this plugin.
- gb 15y agoI'm not disagreeing with your general point, but this does seem a little overkill when you could achieve a similar effect with a few constants: 5 * MINUTE 3.5 * HOUR etc.
- jrockway 15y agoWhere are you getting these constants in JavaScript? You can't even trust the built-in "undefined" to actually contain "undefined". (My point is: a library may be more appropriate in the JavaScript case, because of the lack of constants.)
- snissn 15y agois mytimelibrary.MINUTE what you're getting at?
- jerf 15y agoFor the small benefit of making timeout calls slightly prettier, I incur the small-but-probably-net-greater risk that there is some bug, subtle or otherwise, in this code, along with a motley assortment of other issues I usually gloss over but start being significant when we're talking about such small benefits, like adding a dependency and having to worry about the licensing situation. (Even just having to worry about something that turns out to be "public domain" is something to judge.) Net net I'd personally judge it a loss; YMMV, no sarcasm. I would generally analyze any NIH solution that solves such a small problem in the same way. It is, perhaps, one place where NIH is appropriate.
- benatkin 15y agoThe 2 * 60 * 1000 idiom for two minutes doesn't bother me. underscore.js has a nice alternative for setTimeout() and several functions that probably should be used instead of setInterval() in most cases where setInterval() is used: http://documentcloud.github.com/underscore/#delay http://documentcloud.github.com/underscore/#delay
- avk 15y agoThanks for sharing. I haven't worked with underscore yet. What about delay do you find superior to setTimeout() ?
- fuckingromans 15y agowhy doesnt it support roman numerals? this is unacceptable!
- kragen 15y agoWhy would I add 1.8K of possibly buggy minified code, compiled from 247 lines of possibly buggy un-minified code, to my web app, just in order to avoid having to write function after(seconds, fn) { return setTimeout(fn, seconds * 1000); } function every(seconds, fn) { return setInterval(fn, seconds * 1000); } var MINUTE = 60, HOUR = 3600; in my program? I do like the naming and argument order better than the standard functions, but the cost-to-benefit ratio of this library seems off by two orders of magnitude, given the ability to get the same benefit in two or three lines of code instead of 200 or 300.
- avk 15y agoAuthor here. Point taken. Please use whatever works best for you. I mostly wanted to encourage people to write more readable JS timers. Ideally, after() and every() would make it into ECMAScript in some optimized form so you wouldn't need my code :) By the way, the argument order and flexibility is what contributes to most of those lines.
- kqueue 15y agomilliseconds is pretty readable. What's not readable is "9.7" and 100 because I am not sure if that's in seconds, milliseconds unless I know the default.
- kragen 15y agoYou might have a point if we were talking about mass (is that 9.7 grams or 9.7 kilograms?) or length (9.7 feet or 9.7 meters?), but if you're talking about voltage or current or power or time, and using floating-point numbers, there's really only one reasonable default unit you could be using in each case. And the millisecond it is not.
- nitrogen 15y agoAnd the millisecond it is not. ...unless you're talking about computer programming instead of physics, in which case one uses counts of milliseconds, microseconds, or nanoseconds. Also remember that integers are valid floating point numbers, so after(100) is ambiguous in the context of computer programming (as pointed out by the prior poster).
- deleted 15y ago[deleted]
- randall 15y agoI use Jquery Timers to accomplish a similar goal. http://plugins.jquery.com/project/timers http://plugins.jquery.com/project/timers