TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Stop writing milliseconds and hating JavaScript's setTimeout & setInterval

21 点作者 avk大约 14 年前

5 条评论

kragen大约 14 年前
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 未加载
stephenr大约 14 年前
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 未加载
benatkin大约 14 年前
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 未加载
avk大约 14 年前
Author here. Very open to feedback &#38; criticism. Thanks!
评论 #2548955 未加载
randall大约 14 年前
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>