Javascript作用域错误问题
Javascript scope error issue
我编写了这段启动计时器的代码。我触发一个函数,当它达到0时重新启动计时器。它工作,但我得到一个错误在控制台中说Uncaught TypeError:不能读取属性'restartTimer'的undefined。它与this.restartTimer();
有关timer = utility.math.surveyTimer({
seconds: time,
onUpdateStatus: function(remainingTime) {
$(surveyTimerNode).text(remainingTime);
},
restartTimer: function() {
window.TimerInterval = timer.start();
},
onCounterEnd: function() {
if (utility.bool.isQuestionScreen()) {
if (utility.bool.surveyWillLoop()) {
data.setPersistentSurveyData('DSM_SURVEY_SCREENS', surveyScreens);
data.setPersistentSurveyData('DSM_SURVEY_SCREEN_ORDER', surveyScreenOrder);
tagData = data.getPersistentSurveyData('DSM_SURVEY_DATA');
apiParam = api.helper.buildAPIParam('surveyTimeout', tagData);
api.post.postToAPI(apiParam);
parent.resetSurveyProgress();
parent.moveToNextScreen();
this.restartTimer();
} else {
parent.goToEndscreen();
}
}
}
});
window.TimerInterval = timer.start();
JSLint中没有错误,只是运行时错误。奇怪的是它还能用,计时器还会重置。如何删除此错误?下面是实际执行计时器计数的函数:
this.surveyTimer = function (options) {
var timer,
instance = this,
minutes,
secondsMinusMinutes,
remainingTime,
seconds = options.seconds || 30,
updateStatus = options.onUpdateStatus || function () {
return undefined;
},
counterEnd = options.onCounterEnd || function () {
return undefined;
};
function zeroPad(n) {
return (n < 10) ? ("0" + n) : n;
}
function decrementCounter() {
minutes = Math.floor(seconds / 60);
secondsMinusMinutes = seconds - minutes * 60;
remainingTime = minutes + ':' + zeroPad(secondsMinusMinutes);
updateStatus(remainingTime);
if (seconds === 0) {
counterEnd();
instance.stop();
}
seconds -= 1;
}
this.start = function () {
clearInterval(timer);
timer = 0;
seconds = options.seconds;
timer = setInterval(decrementCounter, 1000);
return timer;
};
this.stop = function () {
clearInterval(timer);
};
return this;
};
只是猜测,但我认为您在选项对象中使用的this
不是您认为的this
…
尝试改变你的surveyTimer的实现,它目前在哪里:
counterEnd();
:
counterEnd.call(options);
相关文章:
- 我该如何解决这个问题?“未捕获的类型错误:无法读取 null 的属性'appendChild'”
- 遇到400错误请求(Angular+WebAPI)的问题
- 扩展错误es6类模块问题
- Createjs冲突问题localToLocal不是函数错误
- Ckeditor出现格式错误的html问题
- 用Javascript为QCM动态生成问题——未捕获类型错误
- 访问模板中的属性时出现问题-除'name'显示错误'没有定义[attributename];
- Meteor上有错误的同步问题
- AJAX自定义错误处理代码问题
- Javascript在每个有问题的控件上输出一条错误消息
- 未捕获的语法错误:意外的令牌:跨域问题
- 我在帖子#index中得到以下错误ExecJS::ProgramError我该如何修复此问题
- 防止在php中缓存500内部错误页面的问题
- 代码有什么问题?它给了我一个运行时错误
- NaN 错误代码有什么问题
- Firefox SDK:包括jquery.flot在内的问题,错误显示维度高度为空
- 这个代码出了什么问题?错误丢失”)&”;
- Javascript变量作用域问题-错误:类型错误:this.graphics未定义
- jQuery修复了" Uncaught TypeError: $不是一个函数"的问题错误
- 解决在angular模板中包含脚本标签导致“同步XMLHttpRequest..”的问题;错误感觉很脏