$.get
每当用户输入输入时,我都会使用jquery函数。我的功能看起来像这样
function checkField(va) {
$.get(
'/admin-tool',
{
ctrl : 'checker',
value : va
},
function(d) {
if($('.answer',d).text() != '1') {
$('.main h1').text('Something went wrong, Read the instructions carefuly');
}
});
}
我需要使用该abort()
函数来防止先前的调用在最后一个调用之后完成他的xhr。
所以我做了
var xhrcall;
function checkField(va) {
xhrcall.abort();
xhrcall = $.get(
[...]
});
}
但是我走错了
无法读取未定义的属性“中止”
当然,它没有定义,但是ajax函数甚至不再触发。
我在这里误会什么吗?
我已经为您的代码提供了一些注释。请检查一下。
var xhrcall; // no value assigned here and probably nowhere else -> undefined!
function checkField(va) {
xhrcall.abort(); // so this line causes the error and I believe JS execution stops here!!
xhrcall = $.get(
[...]
});
}
如果您想执行$.get
此操作(基本上,仅在xhrcall
已初始化的情况下才调用abort()):
function checkField(va) {
if (xhrcall) {
xhrcall.abort();
}
xhrcall = $.get(
[...]
});
}
快乐黑客:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句