【问题标题】:Javascript - start and stop with keypress from keyboardJavascript - 通过键盘按键启动和停止
【发布时间】:2015-12-16 16:14:25
【问题描述】:

我有这个 javascript http://jsfiddle.net/ZDsMa/433/ 来选择一个随机数。我把它放在html div上,我想开始用keypress而不是onClick来打乱数字,也用keypress再次停止

 var output, started, duration, desired;

// Constants
duration = 5000;
desired = '5';

// Initial setup
output = $('#output');
started = new Date().getTime();

// Animate!
animationTimer = setInterval(function() {
    // If the value is what we want, stop animating
    // or if the duration has been exceeded, stop animating
    if (output.text().trim() === desired || new Date().getTime() - started > duration) {
        clearInterval(animationTimer);
    } else {
        console.log('animating');
        // Generate a random string to use for the next animation step
        output.text(
            ''+
            Math.floor(Math.random() * 10)+
            Math.floor(Math.random() * 10)
        );
    }
}, 100);

【问题讨论】:

    标签: javascript jquery html javascript-events


    【解决方案1】:

    您可以将动画包装在一个函数中,然后在按键上调用它......就像这样:

    var output, started, duration, desired;
    var sel = [];
    // Constants
    duration = 5000;
    desired = '5';
    
    // Initial setup
    output = $('#output');
    
    var keyPressed = false;
    
    $(document).keypress(function(){
    	if (!keyPressed){
      	started = new Date().getTime();
      	scramble();
        keyPressed = true;
      }
      else{
      	keyPressed = false;
      }
    });
    
    // Animate!
    var scramble = function(){
    	animationTimer = setInterval(function() {
        // If the value is what we want, stop animating
        // or if the duration has been exceeded, stop animating
        if (output.text().trim() === desired || new Date().getTime() - started > duration || !keyPressed) {
        		while($.inArray(output.text().trim(),sel)>-1){
            	output.text(
                  ''+
                  Math.floor(Math.random() * 10)+
                  Math.floor(Math.random() * 10)
              );
            }
            clearInterval(animationTimer);
            keyPressed = false;
            sel.push(output.text());
            $("#selected").text(sel);
        } else {
            
            // Generate a random string to use for the next animation step
            output.text(
                ''+
                Math.floor(Math.random() * 10)+
                Math.floor(Math.random() * 10)
            );
            
        }
    	}, 100);
    }
    #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>
    <div id="selected"></div>

    【讨论】:

    • 太棒了!谢谢!如果我使用 'var Numbers = [129,136,204,322,331] 和 Numbers[ Math.floor( Math.random( ) * 5) ]'跨度>
    • 我编辑了脚本,所以现在当争夺停止时(由于 5 秒或按键),它会查看之前选择的数字,如果已经选择,则搜索另一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多