简体   繁体   English

如何停止setInterval调用

[英]How to stop a setInterval call

I have two HTML buttons. 我有两个HTML按钮。 First button starts the JavaScript function below: 第一个按钮启动以下JavaScript函数:

function generations(){
    var refresh = setInterval(function(){ mutation() }, 1000);
}

Works great. 效果很好。 I want the second button to end the setInterval. 我希望第二个按钮结束setInterval。 I tried like this: 我这样尝试过:

function stop(){
    clearInterval(refresh);
}

Doesn't work. 不起作用 It works if I define the "refresh" variable outside of the function, but then I can't start it when I want to, it just starts automatically. 如果我在函数外部定义了“ refresh”变量,它就可以工作,但是当我想要启动它时,便无法启动它,它会自动启动。 The two functions are defined in a separate .js file. 这两个函数在单独的.js文件中定义。

Help! 救命!

You need to declare it outside and separate the assignment. 您需要在外部声明它并分开分配。 Set the interval inside: 在内部设置间隔:

var refresh;
function generations(){
    refresh = setInterval(function(){ mutation() }, 1000);
}

And stop: 然后停止:

function stop(){
    if (refresh) { 
       clearInterval(refresh); 
    }
}

refresh is confined into the scope of generations() refresh仅限于generations()的范围

You have to make refresh global or let it be passed to stop() as parameter 您必须进行全局refresh或将其作为参数传递给stop()

In your code the "refresh" variable is local only to the function it is located. 在您的代码中,“刷新”变量仅在其所位于的函数中是本地的。 If you to keep it in this manner you should do something like this: 如果您以这种方式保留它,则应执行以下操作:

var refresh;

function generations(){
    refresh = setInterval(function(){ mutation() }, 1000);
}

function stop(){
    clearInterval(refresh);
}

Scopes in Javascript Javascript范围

There are two different scopes available in Javascript, the first of which is called local scope and the second of which is called global scope . Javascript提供了两种不同的作用域 ,第一种称为本地作用域 ,第二种称为全局作用域 A scope defines how much access other functions have to a variable. 范围定义了其他功能对变量的访问量。 Here's how it works (in essence): 实质上是这样的:

  • The var keyword declares a variable at the current scope. var关键字在当前作用域声明一个变量。 Inside a function this is the local scope, outside a function this is the global scope. 在函数内部,这是局部作用域,在函数外部,这是全局作用域。
  • From that point, that variable can only be accessed from within that scope. 从那时起,只能在该范围内访问该变量。
  • Local Scope is only within one function. 本地作用域仅在一个功能内。 A variable in local scope can only be accessed from within that function, and is deleted once it finishes running. 只能在该函数内部访问局部范围内的变量,并且在完成运行后将其删除。
  • Global Scope is within all functions. 全局范围所有功能之内。 It is accessible by all functions, and is only deleted when the browser tab / window is closed (or the Javascript process is killed). 它可以通过所有功能访问,并且仅在关闭浏览器选项卡/窗口(或杀死Javascript进程)时删除。

Note that commonly we like to call variables in the local scope local variables and variables in the global scope global variables . 注意,通常我们喜欢在局部范围内调用局部变量 ,在全局范围内调用全局变量

In your code 在你的代码中

The variable that you declare, refresh , is a local variable because you declare it inline in the generations function. 您声明的变量refresh是局部变量,因为您在generations函数中内联声明了它。 Thus, it is only accessible in the scope of that function, and not to any others. 因此,它只能在该功能范围内访问,其他任何人都不能访问。 This means that it is not able to be used by the stop function. 这意味着stop功能无法使用它。

If you if you want refresh to be accessiable by stop , you need global scope - ie refresh needs to be declared outside of a function. 如果希望通过stop可以访问refresh ,则需要全局作用域-即需要在函数外部声明刷新。 This means that it is accessible for read / write by all other functions in the code. 这意味着代码中的所有其他功能都可以对其进行读取/写入。

An example of how your new code would look (this solves your problem): 新代码的外观示例(这可以解决您的问题):

var refresh; //Declare refresh in the global scope, but don't assign it a value
function generations(){
    refresh = setInterval(function(){ mutation() }, 1000); //Assign a value
}
function stop(){
    clearInterval(refresh); //Read that value
}

One thing to keep in mind is you have to clear your interval before reinitializing it. 要记住的一件事是,您必须在重新初始化间隔之前清除间隔。

Keep the refresh variable in outside scope so you can reference it as well. 将refresh变量保留在外部范围内,以便您也可以引用它。

var refresh;
function generations(){
    if( refresh ){ 
      stop()
    }
    refresh = setInterval(function(){ mutation() }, 1000);
}

function stop(){
    clearInterval(refresh);
}

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

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