jquery 运行 once .one() 在视口中

jquery run once .one() in viewport

我正在制作一个进度条并希望它在视口中播放。我得到了这个工作,但代码现在每次都执行,我只需要 运行 一次。因为它现在创建了多个进度条。 ;-) 以下代码用于 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);