Nea*_*eal 1 javascript jquery object this
我有以下js对象:
var livePage = {
delay: 1000,
loadTables: function(){
loadTable($("#vbeTable"),'getUpdateA')
loadTable($("#vbcTable"),'getUpdateB')
createAlertDialog();
},
setClicks: function(){
$(".expand").live('click',function(){
expand($(this).attr('expandvalue'));
})
$( ".launch" )
.click(function(){
newPopup('index.php',1120,550);
});
$('.edit').live('click',function(){
openColPick($(this).attr('colType'))
});
},
setRightClick: function(){
$('body').contextMenu('mainmenu', {
bindings: {
'o_o': function(t) {
thePopupWindowsMain('oo','','',220,150,'right','');
},
'o_h': function(t) {
thePopupWindowsMain('oh','','',285,385,'left','');
},
'launch_prog': function(t) {
$(".launch").click();
},
'logout': function(t){
window.top.location = 'logout.php';
}
}
});
},
setWindow: function(){
$(window)
.resize(function() {
$('body').css('height', $(this).height())
alertToCorner();
})
.scroll(function(){$(this).resize()});
$(window).resize();
},
checkLogout: function(){
$.ajax({
url: 'getLogin.php',
dataType: "html",
success: function(data){
if($.trim(data) == 'LOGOUT'){
window.location = 'logout.php';
}
},
complete: function(){
setTimeout( function () {
livePage.checkLogout();},
livePage.delay)
},
timeout: 2000
});
},
init: function(){
this.checkLogout();
this.loadTables();
this.setClicks();
this.setRightClick();
this.setWindow();
console.log(this);
}
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,checkLogout: function()我必须使用livePage.delay和livePage.checkLogout() 当我尝试使用时,this.checklogout()我在Chrome的控制台中收到以下错误:
未捕获的TypeError:对象[对象DOMWindow]没有方法'checkLogout'
我该如何解决?
谢谢!
函数内部this不再绑定到外面绑定的任何东西.最简单的解决方案是使用var self = this;或在您的情况下通过context: this选项传递给另一个var $.ajax().