Skip to content

Commit

Permalink
Added .destroy method
Browse files Browse the repository at this point in the history
  • Loading branch information
lcdsantos committed Nov 15, 2017
1 parent 645f9ea commit 7fd072e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
14 changes: 12 additions & 2 deletions dist/menuspy.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ var MenuSpy = function MenuSpy(element, options) {
this.options = utils.extend(defaults, options);

this.assignValues();
window.addEventListener('resize', utils.debounce(function () { return this$1.assignValues(); }));
this.debouncedAssignValuesFn = utils.debounce(function () { return this$1.assignValues(); });
window.addEventListener('resize', this.debouncedAssignValuesFn);

this.debouncedHashFn = utils.debounce(function () {
var hash = this$1.lastInViewElm ? ("#" + (this$1.lastInViewElm.id)) : '#';
Expand All @@ -109,6 +110,7 @@ MenuSpy.prototype.assignValues = function assignValues () {
this.lastInViewElm = null;
this.menuHeight = this.element.offsetHeight + this.options.threshold;
this.menuItems = [].slice.call(this.element.querySelectorAll(this.options.menuItemSelector));
this.raf = null;
};

MenuSpy.prototype.cacheItems = function cacheItems () {
Expand Down Expand Up @@ -176,7 +178,15 @@ MenuSpy.prototype.scrollFn = function scrollFn () {
this.tick();
}

window.requestAnimationFrame(this.scrollFn.bind(this));
this.raf = window.requestAnimationFrame(this.scrollFn.bind(this));
};

MenuSpy.prototype.destroy = function destroy () {
if (this.raf) {
window.cancelAnimationFrame(this.raf);
}

window.removeEventListener('resize', this.debouncedAssignValuesFn);
};

return MenuSpy;
Expand Down
2 changes: 1 addition & 1 deletion dist/menuspy.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions src/menuspy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class MenuSpy {
this.options = utils.extend(defaults, options);

this.assignValues();
window.addEventListener('resize', utils.debounce(() => this.assignValues()));
this.debouncedAssignValuesFn = utils.debounce(() => this.assignValues());
window.addEventListener('resize', this.debouncedAssignValuesFn);

this.debouncedHashFn = utils.debounce(() => {
const hash = this.lastInViewElm ? `#${this.lastInViewElm.id}` : '#';
Expand All @@ -41,6 +42,7 @@ class MenuSpy {
this.lastInViewElm = null;
this.menuHeight = this.element.offsetHeight + this.options.threshold;
this.menuItems = [].slice.call(this.element.querySelectorAll(this.options.menuItemSelector));
this.raf = null;
}

cacheItems() {
Expand Down Expand Up @@ -104,7 +106,15 @@ class MenuSpy {
this.tick();
}

window.requestAnimationFrame(this.scrollFn.bind(this));
this.raf = window.requestAnimationFrame(this.scrollFn.bind(this));
}

destroy() {
if (this.raf) {
window.cancelAnimationFrame(this.raf);
}

window.removeEventListener('resize', this.debouncedAssignValuesFn);
}
}

Expand Down

0 comments on commit 7fd072e

Please sign in to comment.