简体   繁体   English

Javascript-多次调用一个函数,但只有一个作用

[英]Javascript - call a function multiple times but only have one effect

I know I can easily work around this, but I'm looking for the best practice in this case. 我知道我可以轻松解决此问题,但是在这种情况下,我正在寻找最佳实践。 This is a simplified version http://jsbin.com/isered/3/edit . 这是http://jsbin.com/isered/3/edit的简化版本。 I'm trying create a function for reuse but I need it so that when the event is triggered (ie clicking on the anchor tag), it only appends the output area once, not for each time the function has been called. 我正在尝试创建一个函数以供重用,但我需要它,以便在触发事件时(即单击定位标记),它仅将输出区域附加一次,而不是在每次调用函数时附加一次。

Jquery/Javascript jQuery的/ JavaScript的

$(function () {

   function foo () {
      $('a').on('click', function () {
      $('.asdf').append('poo');
    });
   }

   foo ();
   foo ();
});

HTML HTML

<a href='#'>hello</a>
<a href='#'>world</a>

<p class='asdf'></p>

You can make a state variable, in this case a class: 您可以将状态变量设为类:

$(function () {
  function foo() {
    $('a:not(.clicked)').on('click', function() {
      $(this).addClass('clicked');
      $('.asdf').append('poo');
    });
  }

  foo();
  foo();
});

You could unbind the events before binding them: 您可以在绑定事件之前取消绑定事件:

function foo () {
  $('a').off('click').on('click', function () {
      $('.asdf').append('poo');       
  });
}

Though I don't understand why calling the function once and only once is not the best solution. 虽然我不明白为什么一次调用一次函数不是最佳解决方案。

I usually encounter this when I want to make a container only once but process the rest of the function on subsequent clicks as well. 当我只想制作一个容器但在随后的单击中也处理其余功能时,通常会遇到这种情况。 In that case, I put a check for the container's existence as follows: 在这种情况下,我将检查容器的存在情况,如下所示:

var foo = function(){
    $('a').on('click', function(ev){
       if (!$('.asdf .foo').length) {
           $('.asdf').append('<div class="foo">');
       }
       // do other click stuff here
    });
};

Underscore.js already has this function. Underscore.js已经具有此功能。

So why re-invent the wheel. 那么为什么要重新发明轮子呢?

_.once(function) Creates a version of the function that can only be called one time. _.once(function)创建只能调用一次的函数版本。 Repeated calls to the modified function will have no effect, returning the value from the original call. 重复调用修改后的函数将无效,它会返回原始调用中的值。 Useful for initialization functions, instead of having to set a boolean flag and then check it later. 对于初始化功能很有用,而不必设置布尔标志然后再检查它。

Example: 例:

$(function () {

  var foo = _.once(function() {
    $('a').on('click', function () {
      $('.asdf').append('poo');       
    });
  });

  foo ();
  foo ();
});
$(function () {
    function foo () {
        $('a').on('click', function () {
            var num = $('.asdf').text().length;
            if (num == 0) {
                $('.asdf').append('poopsicles');
            }
        });
    }

    foo();
    foo();
});

I guess this question has already been answered, but here ya go. 我想这个问题已经回答了,但是,请继续。

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

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