简体   繁体   English

如何停止和启动功能?

[英]How do I stop and start a function?

I'm just looking to be pointed in the right direction here. 我只是想指出正确的方向。 I'm a beginner with javascript so there's some sort of fundamental thing I'm not understanding. 我是使用javascript的初学者,所以有些基本的东西我不太了解。 Google searches didn't seem to help since apparently this question is never really asked. Google搜索似乎没有帮助,因为显然从未真正询问过此问题。

I have a game where there is a 60 second countdown once the Go button is pressed. 我有一个游戏,一旦按下“ Go”按钮,倒计时为60秒。 Now when that 60 second countdown is running I want to run a function orientationStuff() that does all sorts of stuff on orientationchange. 现在,当60秒倒计时开始运行时,我想运行一个功能directionStuff(),该函数在orientationchange上进行各种处理。 This is no problem...HOWEVER 没问题...但是

I only want orientationStuff() to run during the 60 second countdown. 我只希望directionStuff()在60秒倒计时期间运行。 So once it hits 0, there is no more orientationStuff() going on. 因此,一旦它达到0,就没有更多的directionStuff()了。 BUT once you click Go again I want the countdown to start again (and consequently orientationStuff() 但是一旦您单击“再次转到”,我希望倒数计时再次开始(因此,取向Stuff()

Try something like this in Javascript: 在Javascript中尝试类似的方法:

var i = 0; //counts every frame at 60FPS
function countdown() {
    function frame() {
        i += 1; //up the frame by 1;
        /* do stuff; you can use variable switch-based logic to perform different functions*/
        if(i > 3599) { /* equivilant of 60 seconds, you can add your own condition in if you want*/
            i = 0;
            clearInterval(looper_interval); //stops
        }
    }
    var looper_interval = setInterval(frame, 16); //this code runs the function frame() every 16 milliseconds
}

The simple answer is you use setTimeout with a function you want to execute and 60000 milliseconds 简单的答案是将setTimeout与要执行的函数配合使用,并且需要60000毫秒

setTimeout(function(){
   // do whatever you want to happen here
},60000)

In terms of starting this with jQuery, if you had a button with a class go you would use 在与jQuery开始此,如果您有相关的类的按钮方面go你会使用

$(document).on('click','.go',function(){
    setTimeout(function(){
       // Game over man! GAME OVER!!
    },60000)
});

I guess you probably want to stop the timer - say if the player wins the game. 我猜您可能想停止计时器-说玩家是否赢得比赛。 In that case you capture an "id" from the timer and you can use clearTimeout to stop it 在这种情况下,您可以从计时器中捕获“ id”,然后可以使用clearTimeout将其停止

var gameTimerId = null;
$(document).on('click','.go',function(){
    gameTimerId  = setTimeout(function(){
       // Hury, before someone calls clearTimeout
    },60000)
});

/// where the player wins
clearTimeout(gameTimerId);

setTimeout runs the function once. setTimeout运行一次该函数。 If you want to execute a function repeatedly there is a similar function setInterval which you can use too. 如果要重复执行功能,则可以使用类似的函数setInterval

Try using setTimeout for 60 seconds and toggle a boolean value that you watch when providing the orientationStuff() . 尝试使用setTimeout 60秒钟,并切换在提供orientationStuff()时观察到的布尔值。

If the setTimeout is inside a function, you can simply reset the boolean and rerun the function on the button. 如果setTimeout在函数内部,则可以简单地重置布尔值并在按钮上重新运行该函数。

UPDATE : 更新

var orientationAllowed = false;
function orient() {
  orientationAllowed = true;
  setTimeout(function(){
    orientationAllowed = false;
  },60000);
}

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

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