我有这个 Javascript 函数:
function Card(term, def, terms, curTerm) {
this.term = term;
this.def = def;
this.terms = terms;
this.curTerm = curTerm;
this.show = function() {
that = this;
var html = createCard(that.term, that.def);
$('body').append(html);
$('input[type=text]').focus();
$('.answer').on('click', function(event) {
event.preventDefault();
answer = $(this).parent().serializeArray()[0].value;
// answer correct
if (that.term === answer) {
$('.card').addClass('correct');
$('form').replaceWith('<h2>Correct! ' + that.term + '</h2>');
setTimeout(function () {that.destroy(terms, curTerm + 1);}, 1500);
// answer incorrect
} else {
$('.card').addClass('incorrect');
$('form').replaceWith('<h2>Incorrect! ' + that.term + '</h2>');
setTimeout(function () {that.destroy(terms, curTerm);}, 1500);
}
});
};
我遇到的问题是setTimeout(function () {that.destroy(terms, curTerm + 1);}, 1500);
. 最初我有setTimeout(that.destroy(terms, curTerm + 1), 1500);
,但它没有设置它刚刚调用的超时that.destroy
。为什么放入匿名函数时不立即调用它?这和闭包有关系吗?因为似乎我必须创建一个闭包,但我对它们的了解还不够,无法确定。
任何想法将不胜感激。