jQuery - for 循环超时

发布于 2024-12-05 09:27:51 字数 609 浏览 0 评论 0原文

我编写了一些 jQuery 来循环遍历数组中的项目并显示一个随机数,然后继续显示另一个数字。问题是我想在循环内添加一个延迟,以便它显示数字 2 秒,然后移动到下一个。所以你会看到一个随机数,它会保留 2 秒,然后继续下一个。我尝试在循环内设置超时,但没有成功。任何帮助都会非常感谢。

function RandomNumberGenerator(){

    var myArray = new Array(99);

        for (var i=0; i< 99; i++) {
            myArray[i] = Math.floor(Math.random()*100)
            myArrayTwo[i] = Math.floor(Math.random()*100)
        }

        for (var i=0; i< 9; i++) {
            $('li.num').text(function(index) {

            // timeout needs to be here somewhere 

            return (myArray[i]);

            })
        }
});
}

I have written a bit of jQuery to loop through items in a array an display a random number and then move on to display another number. The problem is I want to put a delay inside the loop so that it shows the number for 2 seconds and then moves of to the next. so you see a random number it holds for 2 seconds and then moves on to the next. I have tried putting a timeout inside the loop but that didn't work. Any help would be amazing thanks.

function RandomNumberGenerator(){

    var myArray = new Array(99);

        for (var i=0; i< 99; i++) {
            myArray[i] = Math.floor(Math.random()*100)
            myArrayTwo[i] = Math.floor(Math.random()*100)
        }

        for (var i=0; i< 9; i++) {
            $('li.num').text(function(index) {

            // timeout needs to be here somewhere 

            return (myArray[i]);

            })
        }
});
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

久随 2024-12-12 09:27:51

请注意,这不是最干净/更好的解决方案,但由于没有人发布您所要求的内容,因此这里是 for 循环中延迟的实现:

var delay = 2000; //2 seconds
for (var i=0; i< 9; i++) {
    setTimeout(function(index){
       $('li.num').text(myArray[index]);
    },delay * i,i);

note that this isn't the cleanest/better solution, but since nobody posted what you asked for, here's an implementation of the delay in your for loop :

var delay = 2000; //2 seconds
for (var i=0; i< 9; i++) {
    setTimeout(function(index){
       $('li.num').text(myArray[index]);
    },delay * i,i);
山色无中 2024-12-12 09:27:51

首先,您需要将完成工作的代码(显示随机数并决定是否应该继续)放入其自己的函数中。该函数也可以是 RandomNumberGenerator 中的局部变量:

    var displayNumber = function() {
        $('li.num').text(myArray[i++]);
        if (i < 100) {
            setTimeout(displayNumber, 2000);
        }
    };

上面的函数将一个数字放入 li.num 中,递增 i,并告诉浏览器在两秒内再次调用该函数(使用 setTimeout) -- 但前提是我们显示的少于100 个号码。

因此,当第一次调用此函数 (displayNumber) 时,它会显示一个数字,并设置为在 2 秒后再次调用自身(显示另一个数字,依此类推)。如果已显示 100 个号码,则不会再次设置呼叫,因此此时重复会停止。

所以你现在可以这样做:

    var myArray = new Array(99);
    for (var i=0; i< 99; i++) {
        myArray[i] = Math.floor(Math.random()*100);
    }

    var i = 0;
    var displayNumber = function() {
        $('#foo').text(myArray[i++]);
        if (i < 10) {
            setTimeout(displayNumber, 2000);
        }
    };

    displayNumber(); // to get the ball rolling

First of all you need to put the code that does the work (displays the random number and decides if it should continue) inside its own function. This function can also be a local variable inside RandomNumberGenerator:

    var displayNumber = function() {
        $('li.num').text(myArray[i++]);
        if (i < 100) {
            setTimeout(displayNumber, 2000);
        }
    };

The function above puts a number in li.num, increments i, and tells the browser to call the function again (using setTimeout) in two seconds -- but only if we have shown less than 100 numbers.

So when this function (displayNumber) is called for the first time, it displays one number and sets things up to call itself again after 2 seconds (to display another number, and so on). If it has displayed 100 numbers already it does not set the call up again, so at that point the repetition stops.

So you can now do:

    var myArray = new Array(99);
    for (var i=0; i< 99; i++) {
        myArray[i] = Math.floor(Math.random()*100);
    }

    var i = 0;
    var displayNumber = function() {
        $('#foo').text(myArray[i++]);
        if (i < 10) {
            setTimeout(displayNumber, 2000);
        }
    };

    displayNumber(); // to get the ball rolling
时光是把杀猪刀 2024-12-12 09:27:51

您实际上并不希望超时“在循环中”,您希望在超时结束时更新数字,然后启动另一个超时来更新数字,然后重复,直到您的满足结束条件。

You don't really want the timeout "in the loop", you want to update the number at the end of the timeout, then start another timeout that'll update the number, and repeat, until your end condition is met.

反目相谮 2024-12-12 09:27:51

延迟函数中的代码不起作用。只要您的代码正在运行,浏览器就不会显示任何更新,因此您必须在更改显示之前退出该函数。

因此,您必须将工作分成可以使用 setTimeoutsetInterval 调用的部分。

像这样的东西:

function RandomNumberGenerator(){

  var myArray = new Array(99);

  for (var i = 0; i < 99; i++) {
    myArray[i] = Math.floor(Math.random()*100)
    myArrayTwo[i] = Math.floor(Math.random()*100)
  }

  var index = 0;
  var handle = window.setInterval(function() {
    $('li.num').text(myArray[index]);
    if (++index == 100) {
      window.clearInterval(handle);
    }
  }, 2000);
}

Delaying the code in the function doesn't work. The browser won't show any updates as long as your code is running, so you have to exit out of the function before the change shows up.

So, you have to divide the work into pieces that you can call using setTimeout or setInterval.

Something like this:

function RandomNumberGenerator(){

  var myArray = new Array(99);

  for (var i = 0; i < 99; i++) {
    myArray[i] = Math.floor(Math.random()*100)
    myArrayTwo[i] = Math.floor(Math.random()*100)
  }

  var index = 0;
  var handle = window.setInterval(function() {
    $('li.num').text(myArray[index]);
    if (++index == 100) {
      window.clearInterval(handle);
    }
  }, 2000);
}
玩世 2024-12-12 09:27:51

这是我为此使用的相当简洁的构造类型(使用 setTimeout 而不是 setInterval)。

var $li = $('li.num');  // Set a local variable for efficiency

var tloop = function(iter, num_of_iters, delay, $li){
    setTimeout(function(){
        $li.text(Math.floor(Math.random()*100)); 
        if (iter < (num_of_iters - 1))  tloop(iter+1, num_of_iters, delay, $li);
    }, delay);
};

tloop(0, 10, 2000, $li);  // Kick off the display

This is the fairly concise type of construct I would use for this (using setTimeout instead of setInterval).

var $li = $('li.num');  // Set a local variable for efficiency

var tloop = function(iter, num_of_iters, delay, $li){
    setTimeout(function(){
        $li.text(Math.floor(Math.random()*100)); 
        if (iter < (num_of_iters - 1))  tloop(iter+1, num_of_iters, delay, $li);
    }, delay);
};

tloop(0, 10, 2000, $li);  // Kick off the display
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文