gpt4 book ai didi

javascript - jQuery - 类型错误 : callback is undefined

转载 作者:行者123 更新时间:2023-11-28 19:30:13 26 4
gpt4 key购买 nike

我做了一个小的 jQuery 插件来打折,打折后如果我不使用回调函数,我会收到错误:“TypeError:回调未定义”

代码:

$.fn.countdown = function (options,callback) {
var settings=$.extend({
duration : 0,
interval : 1000,
text_before : "",
text_after : "",
disable : false,
}, options );
var callback,
container = $(this[0]).html(settings.text_before + settings.duration + settings.text_after),
countdown = setInterval(function (){
var dd = --settings.duration;
if(settings.disable==true){
clearInterval(countdown);
}
else if (dd > 0){
container.html(settings.text_before + dd + settings.text_after);
} else {
clearInterval(countdown);
callback.call(container);
};
}, settings.interval);
};

示例 1 - 使用回调函数:

$("#timer").countdown({
duration : 3, // Discount start from defined number (in this case 3 seconds) DEFAULT: 0
text_before : "Redirection begins in exactly ", // initial text before number (example: Redirection begins in exactly 3), DEFAULT: blank
text_after : " seconds" // initial text after number (example: 3 seconds), DEFAULT: blank
},
// callback function when discount stop on 0
function(){
this.html("Done counting, redirecting.");
window.location = "http://www.creativform.com";
});

示例 2 - 没有回调函数:

$("#timer").countdown({
duration : 3, // Discount start from defined number (in this case 3 seconds) DEFAULT: 0
text_before : "Redirection begins in exactly ", // initial text before number (example: Redirection begins in exactly 3), DEFAULT: blank
text_after : " seconds" // initial text after number (example: 3 seconds), DEFAULT: blank
});

有什么帮助吗?

最佳答案

试试这个

$.fn.countdown = function (options,callback) {
var settings=$.extend({
duration : 0,
interval : 1000,
text_before : "",
text_after : "",
disable : false,
}, options );
var
container = $(this[0]).html(settings.text_before + settings.duration + settings.text_after),
countdown = setInterval(function (){
var dd = --settings.duration;
if(settings.disable==true){
clearInterval(countdown);
}
else if (dd > 0){
container.html(settings.text_before + dd + settings.text_after);
} else {
clearInterval(countdown);

if (typeof callback === 'function') {
callback.call(container);
}
};
}, settings.interval);
};

关于javascript - jQuery - 类型错误 : callback is undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26974499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com