以下是我的骨干应用程序的代码段。我得到" Uncaught TypeError:this.toastMsg不是一个函数"运行它时出错。但是,如果我直接调用方法this.checkUserAction()它工作正常。当我从setInterval()调用时,它会抛出该错误。
var clock;//Global variable
initialize : function(){
clock = setInterval(this.checkUserAction,1000);
}
checkUserAction : function(){
this.toastMsg("Sucess");
},
//Displays message on top of the window
toastMsg : function(msg){
$(".msgText").text(msg);
$(".alert").show("slide", { direction: "up" }, 1000);
}
获取错误:未捕获TypeError:this.toastMsg不是函数
答案 0 :(得分:0)
替换
clock = setInterval(this.checkUserAction,1000);
使用
var self = this;
clock = setInterval(function(){self.checkUserAction()},1000);