JavaScript 循环 &设置时间间隔

发布于 2024-09-12 16:35:51 字数 969 浏览 5 评论 0原文

该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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-09-19 16:35:52

我不确定你为什么不想坚持第一种方法,但是......

while(地板 < 天花板)
{
     window.setInterval(updateTotalUsers, 1000, 楼层);       
}

这将启动 10 个单独的计时器,每个计时器设置为每秒触发一次,每个计时器设置为增加 floor 并更新 totalUsers,并且它们都不会终止。如果他们开火,您会看到计数每秒增加十,而在第一个示例中则增加一。然而,它们都不会真正触发,因为你将永远循环等待地板到达天花板

JavaScript 是单线程的。脚本执行时,事件(包括计时器)排队。因此,您的 while 循环永远不会终止,因为更新它的计时器事件将等待触发,直到它完成执行!

即使情况并非如此,您仍然会有十个计时器,每个计时器都会永远运行。

您的第一种方法走在正确的轨道上。你只需要停止计时器:

function _initCountUpUsers()
{
    var totalUsers = $('#totalUsers');
    var ceiling = parseInt(totalUsers.html());
    var floor = ceiling - 10;

    function updateTotalUsers()
    {
        if(floor < ceiling)
        {
            totalUsers.html(floor);
            floor++;
        }
        else
        {
          window.clearInterval(timerID); // stop firing the timer once per second
        }
    }

    var timerID = window.setInterval(updateTotalUsers, 1000, floor);
}

如果你正在寻找稍微干净/不太容易出错的方法来编写这个,只需编写一个小辅助例程:

function while_interval(proc, int)
{
  var timerID = setInterval(function()
  {
    if ( !proc() ) clearInterval(timerID);
  }, int);
}

...并像这样调用它:

while_interval(function()
{
  if (floor < ceiling)
  {
    totalUsers.html(floor);
    floor++;
    return true;
  }
}, 1000);

这可以让你抽象出一个一点点的内务代码,但更重要的是,只要您希望间隔循环继续执行,它就会迫使您有意识地返回 true

I'm not sure why you didn't want to stick with your first method, but...

while(floor < ceiling)
{
     window.setInterval(updateTotalUsers, 1000, floor);       
}

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:

function _initCountUpUsers()
{
    var totalUsers = $('#totalUsers');
    var ceiling = parseInt(totalUsers.html());
    var floor = ceiling - 10;

    function updateTotalUsers()
    {
        if(floor < ceiling)
        {
            totalUsers.html(floor);
            floor++;
        }
        else
        {
          window.clearInterval(timerID); // stop firing the timer once per second
        }
    }

    var timerID = window.setInterval(updateTotalUsers, 1000, floor);
}

If you're looking for slightly cleaner / less error-prone way to write this, just code yourself up a little helper routine:

function while_interval(proc, int)
{
  var timerID = setInterval(function()
  {
    if ( !proc() ) clearInterval(timerID);
  }, int);
}

...and call it like so:

while_interval(function()
{
  if (floor < ceiling)
  {
    totalUsers.html(floor);
    floor++;
    return true;
  }
}, 1000);

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.

池予 2024-09-19 16:35:52
function _initCountUpUsers()
{
    var totalUsers = $('#totalUsers'),
        ceiling = parseInt(totalUsers.html()),
        floor = cieling - 10,
        intRef = null;

    function updateTotalUsers()
    {
        if(floor < ceiling)
        {
            totalUsers.html(floor);
            floor++;
        }
        else {
            window.clearInterval(intRef);
        }
    }

    intRef = window.setInterval(updateTotalUsers, 1000);
}

或者

function _initCountUpUsers()
{
    var totalUsers = $('#totalUsers');
    var ceiling = parseInt(totalUsers.html());
    var floor = cieling - 10;
    var int;

    function updateTotalUsers()
    {
        if(floor < ceiling)
        {
            totalUsers.html(floor);
            floor++;
            window.setTimeout(updateTotalUsers, 1000);
        }
    }

    window.setTimeout(updateTotalUsers, 1000);
}
function _initCountUpUsers()
{
    var totalUsers = $('#totalUsers'),
        ceiling = parseInt(totalUsers.html()),
        floor = cieling - 10,
        intRef = null;

    function updateTotalUsers()
    {
        if(floor < ceiling)
        {
            totalUsers.html(floor);
            floor++;
        }
        else {
            window.clearInterval(intRef);
        }
    }

    intRef = window.setInterval(updateTotalUsers, 1000);
}

or

function _initCountUpUsers()
{
    var totalUsers = $('#totalUsers');
    var ceiling = parseInt(totalUsers.html());
    var floor = cieling - 10;
    var int;

    function updateTotalUsers()
    {
        if(floor < ceiling)
        {
            totalUsers.html(floor);
            floor++;
            window.setTimeout(updateTotalUsers, 1000);
        }
    }

    window.setTimeout(updateTotalUsers, 1000);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文