只运行一次 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23859916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:42:25  来源:igfitidea点击:

Run javascript function only once

javascriptjquery

提问by Michael Samuel

I have this simple javascript function:

我有这个简单的 javascript 函数:

<script type="text/javascript">
    var popup = '0';
    if(popup == '0') {
       $(document).ready(function () {     
            $(document).on('click', '.button', function(){
                  alert('test');
                  popup = '1';
            });
       }); 
    }
</script>

<button class="button">Test</button>

I want the function to alert only on the first click but it keeps working although I changed the value of popupto 1

我希望该功能仅在第一次点击时发出警报,但它继续工作,尽管我将 的值更改popup为 1

回答by lesssugar

What you need is .one()function. It makes sure the code is triggered only once for you.

你需要的是.one()功能。它确保代码只为您触发一次。

Docs: http://api.jquery.com/one/

文档:http: //api.jquery.com/one/

Docs example:

文档示例:

$( "#foo" ).one( "click", function() {
  alert( "This will be displayed only once." );
});

回答by Martin Konecny

Write the code as follows:

编写代码如下:

<script type="text/javascript">
    var popup = '0';

     $(document).ready(function () {     
          $(document).on('click', '.button', function(){
             if(popup == '0') {
                alert('test');
                popup = '1';
             }
          });
     }); 
</script>

Once your click listener is set, your previous if statement's location wasn't executed anymore. Only the code inside the on click function.

一旦你的点击监听器被设置,你之前的 if 语句的位置就不再执行了。只有点击功能内的代码。

Alternatively, you can unbind the onClick listener instead of setting popup = '1'. Try the following:

或者,您可以取消绑定 onClick 侦听器而不是设置popup = '1'。请尝试以下操作:

<script type="text/javascript">
    var popup = '0';

     $(document).ready(function () {     
          $(document).on('click', '.button', function(){
              alert('test');

              //unbinds *all* listeners previously registered on "document"
              $(document).unbind('click');
          });
     }); 
</script>

Much cleaner, and as Mathletics has mentioned, cleans unnecessary callbacks from memory.

更干净,正如 Mathletics 所提到的,从内存中清除不必要的回调。

回答by cdvv7788

Try:

尝试:

<script type="text/javascript">
    var popup = '0';

       $(document).ready(function () {     
            $(document).on('click', '.button', function(){
                if(popup == '0') {
                  alert('test');
                }
                  popup = '1';
            });
       }); 
</script>

You had a function that was setting popup to 1 but was never checking its value again. This way it gets checked and should work properly.

您有一个函数将 popup 设置为 1,但从未再次检查其值。这样它就会被检查并且应该可以正常工作。