将参数传递给jQuery Countdown插件中的onExpiry函数

spe*_*z86 1 parameters jquery plugins function countdown

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

 var param = 5;    
 initCount(param);

 function initCount(param) {
     $('selector').countdown({
       onExpiry:
       function endCount(param) {
         alert(param);
       }
     });
 }
Run Code Online (Sandbox Code Playgroud)

我看了一下jquery.countdown.js的完整版本,发现开发人员在第47行和第48行说了这句话:"//没有接收任何参数,'这'是包含的部门"嗯,这还不够好我.应用他的选项,他使用以下代码:(第209行)

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

那么....改变的最佳方式是什么:

onExpiry.apply(target,[])

如果需要,我可以在上面提出的选项中传递我的参数?

思考?

小智 5

我尝试了标记为"正确"的解决方案,但它无效.我发送了一封电子邮件给Countdown插件的作者,他的回复如下.我在我的解决方案中实现了他的响应,它运行得很好!这是正确的方法:


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

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

干杯

基思


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

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);
}
Run Code Online (Sandbox Code Playgroud)