下面的代码..。在setTimeout匿名函数中,引用alert.hide()方法的正确方法是什么?把整个调用写成admin.alert.hide();是正确的吗?或者有更好的方法来引用admin而不必直接调用它?
var admin = (function(jQuery, window, document, undefined) {
return {
loader : (function(admin) {
var fade = 75;
var loader = '#loader';
return {
show : function () {
jQuery(loader).stop().fadeIn(fade);
},
hide : function() {
jQuery(loader).stop().fadeOut(fade);
}
}
})(),
alert : (function() {
var timeout;
var fade = 500;
var milliseconds = 1000;
var alert = '#alert';
return {
timeout : timeout,
show : function(message) {
jQuery(alert).find('p').text(message);
jQuery(alert).stop().fadeIn(fade);
clearTimeout(this.timeout);
this.timeout = setTimeout(function() { }, milliseconds);
},
hide : function() {
jQuery(alert).stop().fadeOut(fade);
}
}
})()
}
})(jQuery, window, document);发布于 2014-06-05 14:40:29
你可以这样做:
return
{
timeout : timeout,
show : function(message)
{
jQuery(alert).find('p').text(message);
jQuery(alert).stop().fadeIn(fade);
clearTimeout(this.timeout);
this.timeout = setTimeout((function() { this.hide(); }).bind(this), milliseconds);
},
hide : function()
{
jQuery(alert).stop().fadeOut(fade);
}
}https://stackoverflow.com/questions/24063088
复制相似问题