TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Stop writing milliseconds and hating JavaScript's setTimeout & setInterval

21 pointsby avkabout 14 years ago

5 comments

kragenabout 14 years ago
Why 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<p><pre><code> function after(seconds, fn) { return setTimeout(fn, seconds * 1000); } function every(seconds, fn) { return setInterval(fn, seconds * 1000); } var MINUTE = 60, HOUR = 3600; </code></pre> in my program?<p>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.
评论 #2549214 未加载
stephenrabout 14 years ago
I'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.<p>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.
评论 #2548944 未加载
评论 #2548904 未加载
评论 #2548939 未加载
评论 #2548972 未加载
benatkinabout 14 years ago
The 2 * 60 * 1000 idiom for two minutes doesn't bother me.<p>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:<p><a href="http://documentcloud.github.com/underscore/#delay" rel="nofollow">http://documentcloud.github.com/underscore/#delay</a>
评论 #2549230 未加载
avkabout 14 years ago
Author here. Very open to feedback &#38; criticism. Thanks!
评论 #2548955 未加载
randallabout 14 years ago
I use Jquery Timers to accomplish a similar goal. <a href="http://plugins.jquery.com/project/timers" rel="nofollow">http://plugins.jquery.com/project/timers</a>