<small id='1qEei'></small><noframes id='1qEei'>

  • <legend id='1qEei'><style id='1qEei'><dir id='1qEei'><q id='1qEei'></q></dir></style></legend>

      <i id='1qEei'><tr id='1qEei'><dt id='1qEei'><q id='1qEei'><span id='1qEei'><b id='1qEei'><form id='1qEei'><ins id='1qEei'></ins><ul id='1qEei'></ul><sub id='1qEei'></sub></form><legend id='1qEei'></legend><bdo id='1qEei'><pre id='1qEei'><center id='1qEei'></center></pre></bdo></b><th id='1qEei'></th></span></q></dt></tr></i><div id='1qEei'><tfoot id='1qEei'></tfoot><dl id='1qEei'><fieldset id='1qEei'></fieldset></dl></div>
        <tfoot id='1qEei'></tfoot>

          <bdo id='1qEei'></bdo><ul id='1qEei'></ul>

        JavaScript:删除事件监听器

        时间:2023-09-05

          <tbody id='gIw8W'></tbody>

          <tfoot id='gIw8W'></tfoot>
        • <small id='gIw8W'></small><noframes id='gIw8W'>

              <bdo id='gIw8W'></bdo><ul id='gIw8W'></ul>
              <legend id='gIw8W'><style id='gIw8W'><dir id='gIw8W'><q id='gIw8W'></q></dir></style></legend>
            • <i id='gIw8W'><tr id='gIw8W'><dt id='gIw8W'><q id='gIw8W'><span id='gIw8W'><b id='gIw8W'><form id='gIw8W'><ins id='gIw8W'></ins><ul id='gIw8W'></ul><sub id='gIw8W'></sub></form><legend id='gIw8W'></legend><bdo id='gIw8W'><pre id='gIw8W'><center id='gIw8W'></center></pre></bdo></b><th id='gIw8W'></th></span></q></dt></tr></i><div id='gIw8W'><tfoot id='gIw8W'></tfoot><dl id='gIw8W'><fieldset id='gIw8W'></fieldset></dl></div>

                • 本文介绍了JavaScript:删除事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试删除侦听器定义中的事件侦听器:

                  I'm trying to remove an event listener inside of a listener definition:

                  canvas.addEventListener('click', function(event) {
                      click++;
                      if(click == 50) {
                          // remove this event listener here!
                      }
                  // More code here ...
                  

                  我怎么能这样做?这 = 事件...

                  How could I do that? this = event...

                  推荐答案

                  你需要使用命名函数.

                  另外,click 变量需要在处理程序之外才能递增.

                  Also, the click variable needs to be outside the handler to increment.

                  var click_count = 0;
                  
                  function myClick(event) {
                      click_count++;
                      if(click_count == 50) {
                         // to remove
                         canvas.removeEventListener('click', myClick);
                      }
                  }
                  
                  // to add
                  canvas.addEventListener('click', myClick);
                  

                  <小时>

                  您可以像这样关闭 click_counter 变量:


                  You could close around the click_counter variable like this:

                  var myClick = (function( click_count ) {
                      var handler = function(event) {
                          click_count++;
                          if(click_count == 50) {
                             // to remove
                             canvas.removeEventListener('click', handler);
                          }
                      };
                      return handler;
                  })( 0 );
                  
                  // to add
                  canvas.addEventListener('click', myClick);
                  

                  通过这种方式,您可以跨多个元素递增计数器.

                  This way you can increment the counter across several elements.

                  如果您不希望这样,并且希望每个人都有自己的计数器,那么请执行以下操作:

                  If you don't want that, and want each one to have its own counter, then do this:

                  var myClick = function( click_count ) {
                      var handler = function(event) {
                          click_count++;
                          if(click_count == 50) {
                             // to remove
                             canvas.removeEventListener('click', handler);
                          }
                      };
                      return handler;
                  };
                  
                  // to add
                  canvas.addEventListener('click', myClick( 0 ));
                  

                  我忘记命名在最后两个版本中返回的处理程序.已修复.

                  这篇关于JavaScript:删除事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:onclick=“"vs 事件处理程序 下一篇:什么时候应该使用 jQuery 的 document.ready 函数?

                  相关文章

                  最新文章

                  <legend id='0b1tn'><style id='0b1tn'><dir id='0b1tn'><q id='0b1tn'></q></dir></style></legend>
                  • <bdo id='0b1tn'></bdo><ul id='0b1tn'></ul>
                  <i id='0b1tn'><tr id='0b1tn'><dt id='0b1tn'><q id='0b1tn'><span id='0b1tn'><b id='0b1tn'><form id='0b1tn'><ins id='0b1tn'></ins><ul id='0b1tn'></ul><sub id='0b1tn'></sub></form><legend id='0b1tn'></legend><bdo id='0b1tn'><pre id='0b1tn'><center id='0b1tn'></center></pre></bdo></b><th id='0b1tn'></th></span></q></dt></tr></i><div id='0b1tn'><tfoot id='0b1tn'></tfoot><dl id='0b1tn'><fieldset id='0b1tn'></fieldset></dl></div>
                  1. <tfoot id='0b1tn'></tfoot>

                      <small id='0b1tn'></small><noframes id='0b1tn'>