From aa92487ed397a32096e03385b6c5c1ac30e8b113 Mon Sep 17 00:00:00 2001 From: Hyunje Alex Jun Date: Fri, 31 Oct 2014 19:17:38 +0000 Subject: [PATCH] Clear intervals if the plugin is destroyed. --- src/perfect-scrollbar.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/perfect-scrollbar.js b/src/perfect-scrollbar.js index 55dfec1..876dce7 100644 --- a/src/perfect-scrollbar.js +++ b/src/perfect-scrollbar.js @@ -57,6 +57,7 @@ return this.each(function () { var settings = $.extend(true, {}, defaultSettings); var $this = $(this); + var isPluginAlive = function () { return !!$this; }; if (typeof suppliedSettings === "object") { // Override default settings with any supplied @@ -544,6 +545,11 @@ function startScrolling() { if (!scrollingLoop) { scrollingLoop = setInterval(function () { + if (!isPluginAlive()) { + clearInterval(scrollingLoop); + return; + } + $this.scrollTop($this.scrollTop() + scrollDiff.top); $this.scrollLeft($this.scrollLeft() + scrollDiff.left); updateGeometry(); @@ -712,6 +718,11 @@ clearInterval(breakingProcess); breakingProcess = setInterval(function () { + if (!isPluginAlive()) { + clearInterval(breakingProcess); + return; + } + if (Math.abs(speed.x) < 0.01 && Math.abs(speed.y) < 0.01) { clearInterval(breakingProcess); return; @@ -769,6 +780,7 @@ $scrollbarYRail.remove(); // clean all variables + $this = $scrollbarXRail = $scrollbarYRail = $scrollbarX =