JQuery 自定义插件 - 回调函数触发得太快
所以我构建了一个自定义对话框 JQuery 插件,但我遇到了回调函数的问题。 我试图在对话框关闭后重新加载页面,但我的问题是页面几乎立即重新加载,所以我只看到对话框一瞬间,而实际上它应该显示 5 秒然后消失。 有什么想法我做错了吗?我希望该消息在页面重新加载之前淡出。
这是我对插件的调用:
$('.messages').myPlugin({
'message' : 'Testing'
},function(){location.reload()})
这是插件脚本:
(function( $ ){
$.fn.myPlugin = function( options, callback ) {
if (typeof callback == 'function') { // make sure the callback is a function
callback.call(this); // brings the scope to the callback
}
var settings = {
//DEFAULT OPTIONS
...OPTIONS IN HERE....
};
if ( options ) {
$.extend(settings, options);
};
return this.each(function() {
.....BUILD THE DIALOG....
});
return this;//Leave this to allow chaining
};
}) (jQuery);
So I have built a custom dialog JQuery Plugin, but am having issues with the callback function.
I am trying to reload the page after the dialog closes, but my problem is the page reloads pretty much right away, so I only see the dialog for a split second, when it should actually be displayed for 5 seconds before fading away.
Any ideas what I am doing wrong? I want the message to fade out prior to the page reloading.
Here is my call to the plugin:
$('.messages').myPlugin({
'message' : 'Testing'
},function(){location.reload()})
And Here is the plugin script:
(function( $ ){
$.fn.myPlugin = function( options, callback ) {
if (typeof callback == 'function') { // make sure the callback is a function
callback.call(this); // brings the scope to the callback
}
var settings = {
//DEFAULT OPTIONS
...OPTIONS IN HERE....
};
if ( options ) {
$.extend(settings, options);
};
return this.each(function() {
.....BUILD THE DIALOG....
});
return this;//Leave this to allow chaining
};
})
( jQuery );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
插件初始化代码立即调用提供的回调。就是这一行:
如果您仍然不明白为什么/如何调用回调,请使用调试器在回调内部放置一个断点并单步执行代码 - 查看调用堆栈,它应该非常清楚。
The plugin init code calls the provided callback immediately. It's this line:
If you still don't understand why/how the callback is being called, use a debugger to put a breakpoint inside of the callback and step through the code — look at the call stack and it should be pretty clear.