开发者

What's the jQuery equivalent to this, and what does it do?

开发者 https://www.devze.com 2023-01-25 16:50 出处:网络
I\'m converting some javascript to jQuery from ExtJS and I don\'t know what this does so I\'m not sure what it converts to...

I'm converting some javascript to jQuery from ExtJS and I don't know what this does so I'm not sure what it converts to...

hideTimeout = setTimeout(this.hideAll.createDelegate(this), delay);

delay = 200

What I'开发者_如何学编程m not sure about is the createDelegate(this)...

update

All the JS is...

    Menu.prototype = {
        init: function () {
            var that = this;

            this.ui.link.bind("mouseover", function (e) {
                that.show();
            });
            this.ui.link.bind("mouseout", function (e) {
                that.hide();
            });

            var subOptions = $("li", this.ui.parent);

            $.each(subOptions, function (el) {
                el = $(el);

                el.bind("mouseover", that.cancelTimeout, this);
                el.bind("mouseout", that.hide, this);
            });
        },
        hideAll: function () {
            $("#hd .nav ul ul").hide();
        },
        show: function () {

            this.hideAll();

            this.cancelTimeout();

            showTimeout = setTimeout((function () {
                this.el.show();
            }).createDelegate(this), delay);
        },
        hide: function () {
            this.cancelTimeout();

            hideTimeout = setTimeout(this.hideAll.createDelegate(this), delay);
        },
        cancelTimeout: function () {
            clearTimeout(hideTimeout);
            clearTimeout(showTimeout);
        }
    };


Because you're in a setTimeout, this will represent the window object.

I don't know ExtJS, but it appears to be creating a delegate handler on the window.

Probably best to reference the ExtJS docs. According to the docs for createDelegate:

Creates a delegate (callback) that sets the scope to obj. Call directly on any function. Example: this.myFunction.cre...


EDIT: I believe it would be called like this:

hideTimeout = setTimeout($.proxy( this.hideAll, this), delay);

It will ensure that when hideAll is called, it will be called in its current context.

You can do the same thing for the anonymous function passed to setTimeout in show:

showTimeout = setTimeout($.proxy(function () {
                 this.el.show();
              }, this), delay);


You can accomplish the same thing with jQuery like so:

hideTimeout = setTimeout(jQuery.proxy(this, "hideAll"), delay);

EDIT: Since the method hideAll doesn't contain a reference to this, you can accomplish this even more simply:

hideTimeout = setTimeout(this.hideAll, delay);
0

精彩评论

暂无评论...
验证码 换一张
取 消