clearInterval同时单击鼠标左键和右键不起作用
问题描述:
myDirectives.directive("mouseholdevent", function() { return { restrict: 'A', link: function (scope, element) { var el = element[0]; el.mousehold = true; var setint = ''; var rightClick = false; ele(element).mousedown(function (e) { setint = setInterval(function() { if (rightClick) { clearInterval(setint); } else { if (element.hasClass("zoom_out")) { scope.zoomOutOnCanvasImage(); } else if (element.hasClass("zoom_in")) { scope.zoomInOnCanvasImage(); } } }, 100); }); ele(element).on("mouseleave mouseup contextmenu", function() { rightClick = true; clearInterval(setint); }); } }; });
答
myDirectives.directive("mouseholdevent", function() { return { restrict: 'A', link: function (scope, element) { var setint = ''; ele(element).mousedown(function (e) { clearInterval(setint); if (e.button === 2) { return false; } setint = setInterval(function() { if (element.hasClass("zoom_out")) { scope.zoomOutOnCanvasImage(); } else if (element.hasClass("zoom_in")) { scope.zoomInOnCanvasImage(); } }, 100); }); ele(element).on("mouseleave mouseup", function() { clearInterval(setint); }); } }; });