jquery在视口中运行一次.one()

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

(function ($) { $(document).ready(function() { // Function that checks if it is in view. $("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的示例,而点击不是我想要的但准备好应该做的但没有任何效果。

您可以使用带有"once" $.Callbacks()作为参数。

 (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. $("id ?>").waypoint(function() { callbacks.fire(runOnce); }, { offset: '50%' }); }); })(jQuery);