Skip to content

Commit a80394c

Browse files
author
Johan Steffner
committed
only defer first event dispatch
1 parent 6bf6fa9 commit a80394c

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "responsive",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "",
55
"main": "responsive.js",
66
"repository": {

responsive.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,33 @@
8181
this.eventEmitter = new EventEmitter();
8282
}
8383

84-
setTimeout(function defer () {
85-
this.listen();
86-
this.update();
87-
}.bind(this));
84+
this.listen();
85+
this.update(true);
8886
}
8987

90-
Responsive.prototype.update = function() {
88+
Responsive.prototype.update = function(deferEvents) {
9189
var _previousBreakpoints = this._currentBreakpoints;
9290
this._currentBreakpoints = _.filter(_.keys(this.breakpoints), function(breakpoint) {
9391
return !!matchMedia(this.breakpoints[breakpoint]).matches;
9492
}, this);
9593

9694
if (!_.isEqual(_previousBreakpoints, this._currentBreakpoints)) {
97-
this.dispatchEvents(this._currentBreakpoints, _previousBreakpoints);
95+
if (deferEvents) {
96+
setTimeout(function defer () {
97+
this.dispatchEvents(this._currentBreakpoints, _previousBreakpoints);
98+
}.bind(this));
99+
}
100+
else {
101+
this.dispatchEvents(this._currentBreakpoints, _previousBreakpoints);
102+
}
98103
}
99104
};
100105

101106
Responsive.prototype.listen = function() {
102107
if (!this.listening) {
103-
window.addEventListener('resize', _.throttle(this.update.bind(this), 1000));
108+
window.addEventListener('resize', _.throttle(function handleResize() {
109+
this.update();
110+
}.bind(this), 1000));
104111
this.listening = true;
105112
}
106113
};

0 commit comments

Comments
 (0)