Javascript 无法使用';在';操作员搜索';某物';未定义
这是我的密码:Javascript 无法使用';在';操作员搜索';某物';未定义,javascript,jquery,ajax,Javascript,Jquery,Ajax,这是我的密码: . . keydown: function(ev) { clearTimeout( $(this).data('timer') ); if ( 'abort' in $(this).data('xhr') ) $(this).data('xhr').abort(); // error here var xhr, timer = setTimeout(function() { xhr = $.ajax({
.
.
keydown: function(ev) {
clearTimeout( $(this).data('timer') );
if ( 'abort' in $(this).data('xhr') ) $(this).data('xhr').abort(); // error here
var xhr, timer = setTimeout(function() {
xhr = $.ajax({
url : '/files/tags_autocomplete.php',
dataType : 'JSON',
success : function (tags) {
$("ul").html(tags.output);
}
});
}, 500);
$(this).data({timer : timer, xhr : xhr});
}
.
.
正如我所评论的,第三行抛出了这个错误:
未捕获类型错误:无法使用“in”运算符搜索未定义类型中的“abort”
如何修复它?更改为:
if ('abort' in $(this).data('xhr') ) $(this).data('xhr').abort();
致:
问题只是检查对象是否有xhr
元素。默认情况下,它不存在,因此它是未定义的
,您要求JS引擎在未定义的
信息中查找导致错误的元素
这就是为什么我添加了检查它是否有.data('xhr')
,因为对于JSundefined
被视为false
,然后我检查data('xhr')
是否有abort
属性
顺便说一下,如果您想在按下该键时停止计时器,最好只清除timeout,它不会运行AJAX调用,因此无需将XHR对象放入元素的数据存储:
if($(this).data('timer')) {
clearTimeout($(this).data('timer'));
}
var timer = setTimeout(function() {
$.ajax({
url : '/files/tags_autocomplete.php',
dataType : 'JSON',
success : function (tags) {
$("ul").html(tags.output);
}
});
}, 500);
$(this).data('timer', timer);
if(window.autocompleteTimer) {
clearTimeout(window.autocompleteTimer);
}
window.autocompleteTimer = setTimeout(function() {
$.ajax({
url : '/files/tags_autocomplete.php',
dataType : 'JSON',
success : function (tags) {
$("ul").html(tags.output);
}
});
}, 500);
甚至更简单(无数据存储):
更改为:
if ('abort' in $(this).data('xhr') ) $(this).data('xhr').abort();
致:
问题只是检查对象是否有xhr
元素。默认情况下,它不存在,因此它是未定义的
,您要求JS引擎在未定义的
信息中查找导致错误的元素
这就是为什么我添加了检查它是否有.data('xhr')
,因为对于JSundefined
被视为false
,然后我检查data('xhr')
是否有abort
属性
顺便说一下,如果您想在按下该键时停止计时器,最好只清除timeout,它不会运行AJAX调用,因此无需将XHR对象放入元素的数据存储:
if($(this).data('timer')) {
clearTimeout($(this).data('timer'));
}
var timer = setTimeout(function() {
$.ajax({
url : '/files/tags_autocomplete.php',
dataType : 'JSON',
success : function (tags) {
$("ul").html(tags.output);
}
});
}, 500);
$(this).data('timer', timer);
if(window.autocompleteTimer) {
clearTimeout(window.autocompleteTimer);
}
window.autocompleteTimer = setTimeout(function() {
$.ajax({
url : '/files/tags_autocomplete.php',
dataType : 'JSON',
success : function (tags) {
$("ul").html(tags.output);
}
});
}, 500);
甚至更简单(无数据存储):
这里的问题是该值没有任何属性。您需要检查
data()
的返回值,以检查它是否未定义
var xhr = $(this).data('xhr');
if(typeof xhr !== 'undefiend' && xhr.abort) {
// do your code here
}
用上述4行代码替换
if
语句。这里的问题是该值没有任何属性。您需要检查data()
的返回值,以检查它是否未定义
var xhr = $(this).data('xhr');
if(typeof xhr !== 'undefiend' && xhr.abort) {
// do your code here
}
用上述4行代码替换
if
语句。问题是,如果用户在500毫秒之前再次键入,$(this).data('xhr')
将是未定义的,因为它尚未设置为ajax请求
由于我们不能在未定义的上使用in
运算符,因此只有在对象上,清除超时和中止任何挂起的请求的正确解决方案是,在检查其是否具有中止
属性之前,只需检查$(this).data('xhr')
是否已设置并且是对象
keydown: function(ev) {
var self = $(this);
clearTimeout(self.data('timer'));
if (typeof self.data('xhr') === 'object' && 'abort' in self.data('xhr')) {
self.data('xhr').abort();
}
var timer = setTimeout(function() {
self.data('xhr', $.ajax({
url: '/files/tags_autocomplete.php',
dataType: 'JSON',
success: function(tags) {
$("ul").html(tags.output);
}
}));
}, 500);
self.data('timer', timer);
问题是,如果用户在500毫秒之前再次键入,$(this).data('xhr')
将是未定义的,因为它尚未设置为ajax请求
由于我们不能在未定义的上使用in
运算符,因此只有在对象上,清除超时和中止任何挂起的请求的正确解决方案是,在检查其是否具有中止
属性之前,只需检查$(this).data('xhr')
是否已设置并且是对象
keydown: function(ev) {
var self = $(this);
clearTimeout(self.data('timer'));
if (typeof self.data('xhr') === 'object' && 'abort' in self.data('xhr')) {
self.data('xhr').abort();
}
var timer = setTimeout(function() {
self.data('xhr', $.ajax({
url: '/files/tags_autocomplete.php',
dataType: 'JSON',
success: function(tags) {
$("ul").html(tags.output);
}
}));
}, 500);
self.data('timer', timer);
$(this).data('xhr')
似乎是变量的最佳候选。@4castle haha(:我刚刚给出了问题的解决方案。但在data-*attributes$(this)中保留对象和/或函数是不对的。data('xhr')
似乎是变量的最佳候选。@4castle haha(:我刚刚给出了问题的解决方案。但在数据中保留对象和/或函数是不正确的-*属性用于查找对象或数组中的属性,我不认为$(this)。数据(“xhr”)
是either@SterlingArcher$(this).data(“xhr”)
是xhr
变量的值,它是$返回的jqXHR
对象。ajax
.Shoot,我以为它是一个数据属性。我的错误。用于在对象或数组中查找属性,我不认为$(this)。数据(“xhr”)
是either@SterlingArcher$(this).data(“xhr”)
是xhr
变量的值,它是$返回的jqXHR
对象。ajax
.Shoot,我以为这是一个数据属性。我的错误。