JavaScript 循环 &设置时间间隔
该js将获取一个值,从中减去X数量,然后累加到该值。
我尝试通过执行一个简单的 while 循环来完成此任务,但我无法让它工作。
我想知道是否有人可以评论下面的 while 循环方法如何或为什么不起作用。
这是我让它工作的唯一方法:
function _initCountUpUsers()
{
var totalUsers = $('#totalUsers');
var ceiling = parseInt(totalUsers.html());
var floor = ceiling - 10;
function updateTotalUsers()
{
if(floor < ceiling)
{
totalUsers.html(floor);
floor++;
}
}
window.setInterval(updateTotalUsers, 1000, floor);
}
非工作 While 方法:
function _initCountUpUsers()
{
var totalUsers = $('#totalUsers');
var ceiling = parseInt(totalUsers.html());
var floor = ceiling - 10;
function updateTotalUsers()
{
totalUsers.html(floor);
floor++;
}
while(floor < ceiling)
{
window.setInterval(updateTotalUsers, 1000, floor);
}
}
This js will take a value, subtract X amount from it, and then count up to that value.
I attempted to accomplish this task by doing a simple while loop, but I couldn't get it work.
I was wondering if anyone could comment on how or why the while loop method below doesn't work.
This is the only way I could get it to work:
function _initCountUpUsers()
{
var totalUsers = $('#totalUsers');
var ceiling = parseInt(totalUsers.html());
var floor = ceiling - 10;
function updateTotalUsers()
{
if(floor < ceiling)
{
totalUsers.html(floor);
floor++;
}
}
window.setInterval(updateTotalUsers, 1000, floor);
}
Non-working While method:
function _initCountUpUsers()
{
var totalUsers = $('#totalUsers');
var ceiling = parseInt(totalUsers.html());
var floor = ceiling - 10;
function updateTotalUsers()
{
totalUsers.html(floor);
floor++;
}
while(floor < ceiling)
{
window.setInterval(updateTotalUsers, 1000, floor);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定你为什么不想坚持第一种方法,但是......
这将启动 10 个单独的计时器,每个计时器设置为每秒触发一次,每个计时器设置为增加 floor 并更新 totalUsers,并且它们都不会终止。如果他们开火,您会看到计数每秒增加十,而在第一个示例中则增加一。然而,它们都不会真正触发,因为你将永远循环等待地板到达天花板!
JavaScript 是单线程的。脚本执行时,事件(包括计时器)排队。因此,您的 while 循环永远不会终止,因为更新它的计时器事件将等待触发,直到它完成执行!
即使情况并非如此,您仍然会有十个计时器,每个计时器都会永远运行。
您的第一种方法走在正确的轨道上。你只需要停止计时器:
如果你正在寻找稍微干净/不太容易出错的方法来编写这个,只需编写一个小辅助例程:
...并像这样调用它:
这可以让你抽象出一个一点点的内务代码,但更重要的是,只要您希望间隔循环继续执行,它就会迫使您有意识地返回
true
。I'm not sure why you didn't want to stick with your first method, but...
This is going to start 10 separate timers, each set to fire once a second, each set to increment floor and update totalUsers, and none of them ever terminating. If they were to fire, you'd see the count increase by ten each second, vs. by one in your first example. However, none of them will actually fire, since you'll loop forever waiting for floor to reach ceiling!
JavaScript is single-threaded. Events (including timers) are queued up while a script is executing. So your
while
loop will never terminate because the timer events that would update it will be waiting to fire until it finishes executing!Even if that weren't the case, you'd still have ten timers each running forever.
You were on the right track with your first method. You just need to stop the timer:
If you're looking for slightly cleaner / less error-prone way to write this, just code yourself up a little helper routine:
...and call it like so:
This lets you abstract away a tiny bit of housekeeping code, but more importantly it forces you to consciously return
true
as long as you wish the interval-loop to keep executing.或者
or