简体   繁体   English

jQuery / Javascript随机数动画

[英]jQuery/Javascript Random Number Animation

I'm trying to make an animation where 6 numbers are generated one after another. 我正在尝试制作动画,其中6个数字一个接一个地生成。 However I'm having problems with it. 但是我遇到了问题。 I'm using the code from here http://jsfiddle.net/ZDsMa/1/ 我正在使用这里的代码http://jsfiddle.net/ZDsMa/1/

However I can't seem to get it to pause the loop. 但是我似乎无法让它暂停循环。 This is my code. 这是我的代码。

var numbers = [12,54,32,45,21,69,20];
for (var i = 0; i < (numbers.length + 1); i++) {
    var duration = 2000;
    var desired = numbers[i];

    output = $('#' + i);
    started = new Date().getTime();

    animationTimer = setInterval(function() {
        if (output.text().trim() === desired || new Date().getTime() - started > duration) {

        } else {
            output.text(
                '' +
                Math.floor(Math.random() * 10) +
                Math.floor(Math.random() * 10)
            );
        }
    }, 100);
}

So what I'm trying to do is generate 12 like the above example, wait for it to complete then generate 54 etc... I'm really struggling with this though... Would love some help :) 所以我要做的就是生成12像上面的例子,等待它完成然后生成54等...我真的很挣这个虽然...会喜欢一些帮助:)

There are multiple issues that need to be fixed to have the code work as you intended: 要使代码按预期工作,需要修复多个问题:

  1. Like pie3636 already explained, you will need to call clearInterval(animationTimer) once you want it to stop . 就像pie3636已经解释过的那样, 一旦你希望它停止就需要调用clearInterval(animationTimer)

  2. To be able to generate the next number after the first has finished, wrap the generation in a function instead of a loop which you can call when the previous iteration finishes. 为了能够在第一个完成后生成下一个数字,将生成包装在函数中而不是在上一次迭代完成时可以调用的循环。

  3. As mentioned in my comment, element IDs have to start with a letter. 正如我的评论中所提到的,元素ID必须以字母开头。

  4. When the generator stopped due to duration it would print some random number instead of the desired one. 当发电机由于持续时间而停止时,它将打印一些随机数而不是所需的随机数。

 var numbers = [12, 54, 32, 45, 21, 69, 20]; function generateNumber(index) { var desired = numbers[index]; var duration = 2000; var output = $('#output' + index); // Start ID with letter var started = new Date().getTime(); animationTimer = setInterval(function() { if (output.text().trim() === desired || new Date().getTime() - started > duration) { clearInterval(animationTimer); // Stop the loop output.text(desired); // Print desired number in case it stopped at a different one due to duration expiration generateNumber(index + 1); } else { output.text( '' + Math.floor(Math.random() * 10) + Math.floor(Math.random() * 10) ); } }, 100); } generateNumber(0); 
 .output { margin: 20px; padding: 20px; background: gray; border-radius: 10px; font-size: 80px; width: 80px; color: white; float: left; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="output" id="output0">--</div> <div class="output" id="output1">--</div> <div class="output" id="output2">--</div> 

If you want the generator to always last for the full duration, delete output.text().trim() === desired || 如果希望生成器始终持续整个持续时间,请删除output.text().trim() === desired || from the if statement in the interval code. 来自区间代码中的if语句。

Once you have created a setInterval in a value, the function it points out is executed periodically and indefinitely. 一旦在值中创建了一个setInterval ,它指出的函数就会定期无限期地执行。 To stop it, you will need to call clearInterval(animationTimer) once you want it to stop. 要停止它,您需要在希望它停止后调用clearInterval(animationTimer)

A possible code doing this would be: 这样做的可能代码是:

var numbers = [12,54,32,45,21,69,20];
for (var i = 0; i < (numbers.length + 1); i++) {
    var duration = 2000;
    var desired = numbers[i];

    output = $('#' + i);
    started = new Date().getTime();

    animationTimer = setInterval(function() {
        if (output.text().trim() === desired || new Date().getTime() - started > duration) {
            clearInterval(animationTimer); // Stops the loop
        } else {
            output.text(
                '' +
                Math.floor(Math.random() * 10) +
                Math.floor(Math.random() * 10)
            );
        }
    }, 100);

    // Stops the animation after one second
    setTimeout(function () { clearInterval(animationTimer); }, 1000);
}

I would prefer setTimeout over setInterval . 我更喜欢setTimeout不是setInterval
The reason why is explained here: setTimeout or setInterval 这里解释了原因: setTimeout或setInterval

 var numbers = [12, 54, 32, 45, 21, 69, 20]; function printNumbers(numbersToPrint) { $("#output").text(numbersToPrint.shift()); if (numbersToPrint.length) { setTimeout(function() { printNumbers(numbersToPrint); }, 500) } } printNumbers(numbers.slice()); // printNumbers changes the passed array. // numbers.slice() "clones" the array so "numbers" will be unchanged 
 #output { margin: 20px; padding: 20px; background: gray; border-radius: 10px; font-size: 80px; width: 80px; color: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="output">--</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM