Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法使用';在';操作员搜索';某物';未定义_Javascript_Jquery_Ajax - Fatal编程技术网

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')
,因为对于JS
undefined
被视为
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')
,因为对于JS
undefined
被视为
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,我以为这是一个数据属性。我的错误。