所以我正在使用 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/