【问题标题】:Passing Parameters to onExpiry Function in jQuery Countdown plugin将参数传递给 jQuery Countdown 插件中的 onExpiry 函数
【发布时间】:2011-08-12 23:53:32
【问题描述】:

所以我使用http://keith-wood.name/countdown.html 进行倒计时,并试图弄清楚如何将一些参数传递给可以在插件选项中设置的回调函数:

 var param = 5;    
 initCount(param);

 function initCount(param) {
     $('selector').countdown({
       onExpiry:
       function endCount(param) {
         alert(param);
       }
     });
 }

我查看了完整版的 jquery.countdown.js,发现开发者在第 47 行和第 48 行字面意思是这样说的:"// 不接收参数,'this' 是包含的除法" 好吧,这对我来说还不够好。应用他的选项,他使用以下代码:(第 209 行)

var onExpiry = this._get(inst, 'onExpiry'); 如果(到期){ onExpiry.apply(目标, []); }

所以.... 最好的改变方法是什么:

onExpiry.apply(目标, [])

以便我可以在上面建议的选项中根据需要传递我的参数?

想法?

【问题讨论】:

    标签: jquery function plugins parameters countdown


    【解决方案1】:

    我尝试了标记为“正确”的解决方案,但没有成功。我给 Countdown 插件的作者发了一封邮件,他的回复如下。我在我的解决方案中实施了他的回应,并且效果很好!以下是正确的做法:


    您可以通过匿名函数传递额外的参数。像这样的:

    $('#countdown').countdown({until: +300, onExpiry: function() {
        myFunction(createdTime, searchText, updateDestination);
    }});    
    

    干杯

    基思


    顺便说一下,这是我的应用程序的工作代码:

    function MakeTimer(timerNode, displayNode, searchTerm){
        // get the duration of the timer
        var duration = Number( $("#refreshTimer").val() );
    
        // create a new property and/or set property to creation time
        timerNode.prop('createdTime', new Date());
        timerNode.prop('searchTerm', searchTerm);
    
        //assumes that timerNode is a jQuery node and duration is an int
        //timerNode.countdown({until: duration, format: "s", compact: true, onExpiry: resetTimer});
        timerNode.countdown({until: duration, format: "s", compact: true, onExpiry: function() {
            resetTimer(timerNode, displayNode);
        }});
    
        // get twitter data
        getTwitterData(timerNode, displayNode);
    }
    
    function resetTimer(timerNode, displayNode){
        // get the current duration of the timer
        var duration = Number( $("#refreshTimer").val() );
        timerNode.countdown('change','until', duration);
    
        // get updated twitter data
        getTwitterData(timerNode, displayNode);
    }
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,感谢上帝,我刚刚在这里找到了一个解决方案: 您必须在 Jquery.countdown.js 中的选项签名中添加一个新参数:

      this._defaults = {
              until: null, // new Date(year, mth - 1, day, hr, min, sec) - date/time to count down to
                  // or numeric for seconds offset, or string for unit offset(s):
                  // 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
              since: null, // new Date(year, mth - 1, day, hr, min, sec) - date/time to count up from
                  // or numeric for seconds offset, or string for unit offset(s):
                  // 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
              timezone: null, // The timezone (hours or minutes from GMT) for the target times,
                  // or null for client local
              serverSync: null, // A function to retrieve the current server time for synchronisation
              format: 'dHMS', // Format for display - upper case for always, lower case only if non-zero,
                  // 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds
              layout: '', // Build your own layout for the countdown
              compact: false, // True to display in a compact format, false for an expanded one
              significant: 0, // The number of periods with values to show, zero for all
              description: '', // The description displayed for the countdown
              expiryUrl: '', // A URL to load upon expiry, replacing the current page
              expiryText: '', // Text to display upon expiry, replacing the countdown
              alwaysExpire: true, // True to trigger onExpiry even if never counted down
              onExpiry: null, // Callback when the countdown expires -
                  // receives no parameters and 'this' is the containing division
              onTick: null, // Callback when the countdown is updated -
                  // receives int[7] being the breakdown by period (based on format)
                  // and 'this' is the containing division
              tickInterval: 1 ,// Interval (seconds) between onTick callbacks
              identifier:''
          };
      

      那么你应该在函数调用中添加你的标识符

      if (onExpiry) {
                          onExpiry.apply(target,[this._get(inst, 'identifier')]);
                      }
      

      那么对 UpdateCountdown 的调用将如下所示

      $('#defaultCountdown).countdown({identifier:1,onExpiry: yourFuncion, until: new Date('2012-01-04')});
      

      您的函数将如下所示:

      function yourFunction(id){
      alert(id);
      }
      

      您可以将 Jquery.countdown.js 上的 alwaysExpire 设置为 true ,这样您就可以测试提前过期的日期。

      【讨论】:

      • 我在等待您的反应;)我很高兴它最终变得有用。 ;)
      猜你喜欢
      • 2013-01-27
      • 2010-09-27
      • 1970-01-01
      • 2016-11-08
      • 2018-04-07
      • 2011-04-16
      • 2017-02-05
      • 2014-10-18
      • 2013-09-25
      相关资源
      最近更新 更多