1

我正在尝试在窗口的调整大小事件上设置去抖动,并且以前能够使用下划线和 jquery 如下:

$(window).on('resize', _.debounce($.proxy(this.windowWidthChanged, this), 333));

我采取了这种想法并尝试将其应用于 Ember Ember.run.debounce,如下所示:

$(window).on('resize', Ember.run.debounce(this, this.windowWidthChanged, 333));

事件监听器似乎根本没有触发......

4

2 回答 2

3

就像您可能已经猜到的那样,您没有将函数传递给 resize 事件,而是传递了取消信息(调用 debounce 的结果)。

var self = this;
$(window).on('resize', function(){ Ember.run.debounce(self, self.windowWidthChanged, 333)});

这又回到了经典的 setTimeout 困境,为什么它会立即运行?

setTimeout(alert('hello'),2000)
于 2013-12-14T16:32:18.913 回答
1

我最终将它包装在代理内的匿名函数中以维护this上下文:

$(window).on('resize', $.proxy(function() {
    Ember.run.debounce(this, this.windowWidthChanged, 333);
}, this));

我想你也可以移动Ember.run.debounce内部windowWidthChanged

于 2013-12-14T06:21:48.657 回答