From 59d6c23c508c1ee8a0e12152a7ee9810d558401a Mon Sep 17 00:00:00 2001 From: zozzz Date: Sun, 11 May 2014 00:01:42 +0200 Subject: [PATCH] Move mouseLocs track code outside from init If init function called a lot of times, the browser dramatically slow down, because the mouse location track function called every time of mouse move * init function call count. --- jquery.menu-aim.js | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/jquery.menu-aim.js b/jquery.menu-aim.js index 0c32941..0f471bb 100644 --- a/jquery.menu-aim.js +++ b/jquery.menu-aim.js @@ -70,6 +70,9 @@ * https://github.com/kamens/jQuery-menu-aim */ (function($) { + var mouseLocs = [], + MOUSE_LOCS_TRACKED = 3, // number of past mouse locations to track + DELAY = 300; // ms delay when user appears to be entering submenu; $.fn.menuAim = function(opts) { // Initialize menu-aim for all elements in jQuery collection @@ -83,7 +86,6 @@ function init(opts) { var $menu = $(this), activeRow = null, - mouseLocs = [], lastDelayLoc = null, timeoutId = null, options = $.extend({ @@ -98,20 +100,6 @@ exitMenu: $.noop }, opts); - var MOUSE_LOCS_TRACKED = 3, // number of past mouse locations to track - DELAY = 300; // ms delay when user appears to be entering submenu - - /** - * Keep track of the last few locations of the mouse. - */ - var mousemoveDocument = function(e) { - mouseLocs.push({x: e.pageX, y: e.pageY}); - - if (mouseLocs.length > MOUSE_LOCS_TRACKED) { - mouseLocs.shift(); - } - }; - /** * Cancel possible row activations when leaving the menu entirely */ @@ -315,9 +303,17 @@ .mouseenter(mouseenterRow) .mouseleave(mouseleaveRow) .click(clickRow); + }; - $(document).mousemove(mousemoveDocument); + /** + * Keep track of the last few locations of the mouse. + */ + $(document).mousemove(function(e) { + mouseLocs.push({x: e.pageX, y: e.pageY}); - }; + if (mouseLocs.length > MOUSE_LOCS_TRACKED) { + mouseLocs.shift(); + } + }); })(jQuery);