Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove window event listeners when plugin is destroyed #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions background-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
'use strict';

var resizeEvent = window.orientation !== undefined ? 'orientationchange' : 'resize';
var resizeFn;
var scrollFn;
var supported;
var canvas;
var context;
Expand Down Expand Up @@ -70,12 +72,14 @@
canvas.style.width = '100%';
canvas.style.height = '100%';

window.addEventListener(resizeEvent, throttle.bind(null, function () {
resizeFn = throttle.bind(null, function () {
resizeCanvas();
check();
}));
});
window.addEventListener(resizeEvent, resizeFn);

window.addEventListener('scroll', throttle.bind(null, check));
scrollFn = throttle.bind(null, check);
window.addEventListener('scroll', scrollFn);

resizeCanvas();
check();
Expand All @@ -88,6 +92,13 @@
* Destructor
*/
function destroy() {
if (supported) {
window.removeEventListener(resizeEvent, resizeFn);
window.removeEventListener('scroll', scrollFn);
resizeFn = null;
scrollFn = null;
}

supported = null;
canvas = null;
context = null;
Expand Down Expand Up @@ -241,7 +252,7 @@
*/
function kill(start) {
var duration = new Date().getTime() - start;

log('Duration: ' + duration + 'ms');

if (duration > get('maxDuration')) {
Expand Down Expand Up @@ -540,7 +551,7 @@
for (var t = 0; t < targets.length; t++) {
target = targets[t];
target = get('changeParent') ? target.parentNode : target;

classList(target, get('classes').light, 'remove');
classList(target, get('classes').dark, 'remove');
classList(target, get('classes').complex, 'remove');
Expand All @@ -549,7 +560,7 @@


/*
* Calculate average pixel brightness of a region
* Calculate average pixel brightness of a region
* and add 'light' or 'dark' accordingly
*/
function calculatePixelBrightness(target) {
Expand Down
2 changes: 1 addition & 1 deletion background-check.min.js

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

Loading