Jquery在视窗中运行一次.one()

jquery run once .one() in viewport

本文关键字:一次 one 运行 Jquery      更新时间:2023-09-26

我正在制作一个进度条,并希望它在视窗中播放。我得到了这个工作,但代码现在每次执行,我需要它只运行一次。因为它现在创建了多个进度条。: -)下面的代码用于Joomla扩展。

(function ($) {
  $(document).ready(function() {
    // Function that checks if it is in view.
    $("<?php echo '#progress' . $module->id ?>").waypoint(function() {
      // Function that makes sure it only runs once.
      // -----------I need to use .one() here but how?
       // The location of the progressbar code for now lets put a alert in.
       alert("run only once");
    }, {
      offset: '50%'
    });
  });
})(jQuery);

我一直在阅读关于。one()函数以及如何在这里使用它。但是我尝试了使用clickready的例子,而点击不是我想要的,但准备好应该做的技巧,但没有工作。

可以使用$.Callbacks(), "once"作为参数

(function ($) {
  $(document).ready(function() {
    function runOnce() {
      // Function that makes sure it only runs once.
      // -----------I need to use .one() here but how?
       // The location of the progressbar code for now lets put a alert in.
       alert("run only once");
    }
    var callbacks = $.Callbacks("once");
    callbacks.add(runOnce);
    // Function that checks if it is in view.
    $("<?php echo '#progress' . $module->id ?>").waypoint(function() {
      callbacks.fire(runOnce);
    }, {
      offset: '50%'
    });
  });
})(jQuery);