[英]How do I call a function from within a nested function in jQuery?
我有以下jQuery代码。 调用this.Next()函数时会引发“不是函数”错误。 在与this.countdown()相同的函数内调用this.Next()函数。 我将如何在setInterval()嵌套函数中调用this.Next()?
this.countdown = function(element) {
interval = setInterval(function() {
var el = document.getElementById(element);
if(seconds == 0) {
if(minutes == 0) {
alert(el.innerHTML = "Your time for this section has expired");
clearInterval(interval);
// Send user to the next section
return this.Next();
} else {
minutes--;
seconds = 60;
}
}
if(minutes > 0) {
var minute_text = minutes + ' min';
} else {
var minute_text = '';
}
var second_text = 'sec';
el.innerHTML = 'Your time: ' + minute_text + ' ' + seconds + ' ' + second_text;
seconds--;
}, 1000);
};
this
的价值会改变取决于它被称为上下文。 您应该尝试如下操作(假设您的countdown
功能在MyObject
)
function MyObject() {
var self = this; // add this declaration and instead of this key word, you can use self
self.countdown = function (element) {
....blah blah blah...
return self.Next(); // instead of this.Next(), you use self.Next()
....blah blah blah...
}
}
可能是因为您编写的是Next()而不是next()尝试编写小的'n'。
希望这个能对您有所帮助
谢谢!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.