我是Javascript新手,我正在分析下面的代码。我的问题是:对于setTimeout()函数,它是如何被调用的?它看起来像它只是被分配给变量this.timer,而且它还没有被调用。但是该功能仍然执行。我在想什么/不理解?非常感谢你的帮助!Javascript的执行要求
$(document).ready(function() {
var validateUsername = $('#validateUsername');
$('#username').keyup(function() {
var t = this;
if (this.value != this.lastValue) {
if (this.timer) clearTimeout(this.timer);
validateUsername.removeClass('error').html('<img src="images/ajax-loader.gif" height="16" width="16" /> checking availability...');
this.timer = setTimeout(function() {
$.ajax({
url: 'ajax-validation.php',
data: 'action=check_username&username=' + t.value,
dataType: 'json',
type: 'post',
success: function (j) {
validateUsername.html(j.msg);
}
});
}, 200);
this.lastValue = this.value;
}
});
});
噢好的。因此,为了跟上这一点,如果我要写一行代码,例如:var a = function(){},a()会执行还是仅仅定义它?非常感谢你的帮助! – voltair
如果您只是将一个函数表达式分配给一个变量,而不做其他任何操作,那么它将不会执行。 – Quentin