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

标签 jquery function plugins parameters countdown

所以我正在使用 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'); if (onExpiry) { onExpiry.apply(target, []); }

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

onExpiry.apply(target, [])

这样我可以在上面建议的选项中需要时传递我的参数吗?

想法?

最佳答案

我尝试了标记为“正确”的解决方案,但它不起作用。我给倒计时插件的作者发了一封电子邮件,他的回复如下。我在我的解决方案中实现了他的回应,效果非常好!正确的做法是这样的:

<小时/>

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

$('#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);
}

关于jquery - 将参数传递给 jQuery Countdown 插件中的 onExpiry 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7047516/

相关文章:

我的世界 bukkit 插件的 Java 编程

loops - Ansible 遍历文件

jquery - 限制 HTML 中的用户输入

jquery - 使用 jquery 更改占位符文本字体和颜色

javascript - 将带有参数的 javascript 函数分配给变量会立即被调用

function - clojure 函数循环依赖是设计明确禁止的,还是只是读者行为?

plugins - TYPO3 4.7.2 通过 typescript 包含 extbase 插件

javascript - 使用选择标签进行选择后禁用选择?

javascript - 如何从网站网址获取图像并将所有图像存储在 PC 的文件夹中?

excel - 函数在未被调用的情况下被调用并停止主宏