jQuery 回调到非 jQuery 父对象

发布于 2024-09-08 17:20:30 字数 765 浏览 8 评论 0原文

请参阅此代码:

var MyObject = new function() {

  this.tos = new Array();

  this.show = function() {
    this.clearTimeouts();
    $("#divExample").slideDown(null,function() {
      MyObject.tos[MyObject.tos.length] =
        setTimeout(function(){MyObject.doSomething();} , 1800);
    });
    return;
  };

  this.doSomething = function() {
    return;
  };

  this.clearTimeouts = function(){
    for (var i=0; i<this.tos.length; i++)
      clearTimeout(this.tos[i]);
    this.tos = new Array();
    return;
  };

}

MyObject 及其方法在几个地方使用。也许这是一个不好的方法,我不知道。由于我自己的原因,我不想将它与 jQuery 联系得太紧密,所以这样保留它是有意义的,因为我可以轻松地将幻灯片更改为 style.display。

问题是我不喜欢在 jQuery 幻灯片的回调中将对象引用为 MyObject,但我必须将超时引用添加到我的数组中,以便它们都可以被清除。有更好的方法吗?

谢谢!

See this code:

var MyObject = new function() {

  this.tos = new Array();

  this.show = function() {
    this.clearTimeouts();
    $("#divExample").slideDown(null,function() {
      MyObject.tos[MyObject.tos.length] =
        setTimeout(function(){MyObject.doSomething();} , 1800);
    });
    return;
  };

  this.doSomething = function() {
    return;
  };

  this.clearTimeouts = function(){
    for (var i=0; i<this.tos.length; i++)
      clearTimeout(this.tos[i]);
    this.tos = new Array();
    return;
  };

}

MyObject and it's methods are used in a few places. Maybe it's a bad way to do it, I dunno. I didn't want to tie it too closely with jQuery for my own reasons, so leaving it like this made sense as I can easily change the slide to style.display.

The problem is that I dont like referencing the object as MyObject in the callback of the jQuery slide, but I have to to add the timeout reference to my array of them so they can all be cleared. Is there a better way to do this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能否归途做我良人 2024-09-15 17:20:30

你可以尝试这样的事情:

this.show = function() {
    var obj = this;
    obj.clearTimeouts();
    $("#divExample").slideDown(null,function() {
      obj.tos[obj.tos.length] =
        setTimeout(function(){obj.doSomething();} , 1800);
    });
    return;
  };

You could try something like this:

this.show = function() {
    var obj = this;
    obj.clearTimeouts();
    $("#divExample").slideDown(null,function() {
      obj.tos[obj.tos.length] =
        setTimeout(function(){obj.doSomething();} , 1800);
    });
    return;
  };
空城之時有危險 2024-09-15 17:20:30
var MyObject = (function() {

  // private variable
  tos = new Array();

  // private method
  function doSomething() {
       // do something
       // ..
  }      

  // return an instance with public methods
  return {
    show: function() {
      this.clearTimeouts();
      $("#divExample").slideDown(null,function() {
        tos[tos.length] =
          setTimeout(function(){ doSomething(); } , 1800);
      });
    },
    clearTimeouts: function() {
      for (var i=0; i<tos.length; i++)
        clearTimeout(tos[i]);
      tos = new Array();
    }
  }
}​;​
var MyObject = (function() {

  // private variable
  tos = new Array();

  // private method
  function doSomething() {
       // do something
       // ..
  }      

  // return an instance with public methods
  return {
    show: function() {
      this.clearTimeouts();
      $("#divExample").slideDown(null,function() {
        tos[tos.length] =
          setTimeout(function(){ doSomething(); } , 1800);
      });
    },
    clearTimeouts: function() {
      for (var i=0; i<tos.length; i++)
        clearTimeout(tos[i]);
      tos = new Array();
    }
  }
}​;​
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文