Skip to content

Instantly share code, notes, and snippets.

@danielt69
Last active May 23, 2019 12:15
Show Gist options
  • Save danielt69/ae99ec480461d59cff1635178195e41e to your computer and use it in GitHub Desktop.
Save danielt69/ae99ec480461d59cff1635178195e41e to your computer and use it in GitHub Desktop.
a better (performance) event listener
/* Feature detection */
var passiveIfSupported = false;
try {
window.addEventListener("test", null, Object.defineProperty({}, "passive", { get: function() { passiveIfSupported = { passive: true }; } }));
} catch(err) {}
window.addEventListener('scroll', function(event) {
/* do something */
// can't use event.preventDefault();
console.count('scroll');
}, passiveIfSupported );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment