简体   繁体   English

JavaScript,jQuery和'this':这里发生了什么?

[英]JavaScript, jQuery and 'this' : what's going on here?

This is more a question about what's going on in my code. 这是一个关于我的代码中发生了什么的问题。 It works, but I need a little bit enlightenment... 它有效,但我需要一点启示......

I'm using jQuery to create an event listener on an text input element. 我正在使用jQuery在文本输入元素上创建一个事件监听器。 The HTML would simply be as follows: HTML将简单如下:

<input type="text" id="autocomplete" size="50" />

I need to receive events whenever someone types something into that field, and to avoid being flooded with events I've set the event listener up with a timer so that my event listener code only runs if the user has not type anything for 125 ms: 每当有人在该字段中键入内容时,我都需要接收事件,并且为了避免被事件淹没,我已经使用计时器设置了事件监听器,这样我的事件监听器代码只有在用户没有输入125 ms的任何内容时才会运行:

jQuery('#autocomplete').keyup(function(e) {
  e.preventDefault();
  clearTimeout(this.timer);
  this.timer = setTimeout(function() {
    jQuery.getJSON([URL goes here], function(data) {
      // processing of returned data goes here.
    }
  }, 125);

console.log(this);
});

This works, but I'm not quite sure if I understand everything going on here. 这有效,但我不太确定我是否理解这里发生的一切。 As you see from the above piece of code, I need to keep track of the ID of the timer which was created on the last event. 从上面的代码中可以看出,我需要跟踪在上一个事件中创建的计时器的ID I do that by storing it in this.timer . 我把它存储在this.timer However, the this keyword in this case refers to the input element. 但是,在这种情况下, this关键字指的是input元素。 This is what console.log outputs: 这是console.log输出的内容:

<input type="text" id="autocomplete" size="50" />

Is it OK to store the timer ID on the input element? 可以在输入元素上存储定时器ID吗? For all I know, I could be doing something stupid. 据我所知,我可能会做一些愚蠢的事情。 Is this "best practice", or would that be something very different? 这是“最佳实践”,还是会有不同之处?

In general I prefer not to place any further values directly into the DOM object. 通常,我不希望将任何其他值直接放入DOM对象中。 As the DOM matures, there is a slim chance the namespace you choose could be used by the DOM for other purposes. 随着DOM的成熟,您选择的命名空间很可能被DOM用于其他目的。 As well as this other scripts could cause namespace conflicts, certainly with a variable named timer. 除了这个其他脚本可能导致命名空间冲突,当然还有一个名为timer的变量。 If you must do this then use an obscure namespace to mitigate this risk. 如果必须这样做,那么使用一个模糊的命名空间来降低这种风险。 Older versions of IE also have strange behaviour when it comes to serialising a DOM node, you could end up with an actual attribute in the node called timer with a very odd value. 较旧版本的IE在序列化DOM节点时也有奇怪的行为,您最终可能会在名为timer的节点中找到一个具有非常奇怪值的实际属性。 As well as this, console.log does not return a list of the DOM node properties making debugging more complex than it should be. console.logconsole.log不会返回DOM节点属性的列表,这使得调试比应该更复杂。

In general it is better practice to leave the DOM as it is and reference its properties as defined by the document object. 通常,最好将DOM保持原样并引用文档对象定义的属性。 In my opinion it would be better practice to place the timer into the equivalent of a public static variable within an anonymous function along with your handler, this avoids any scope problems where you may accidentally overwrite your timer and keeps your code modular. 在我看来,最好将定时器放在匿名函数中的公共静态变量和处理程序中,这样可以避免任何可能会意外覆盖定时器并保持代码模块化的范围问题。

(function($) {

    // Static variable for this function
    var keyupTimer;

    // Event handler for keyup
    $('#autocomplete').keyup(function(event) 
    {
        event.preventDefault();

        clearTimeout(keyupTimer);

        keyupTimer = setTimeout(function()
        {
            $.getJSON([URL goes here], function(data)
            {
                // processing of returned data goes here.
            }
        }, 125);
    });
})(jQuery);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM