我在JavaScript中运行一个函数,一旦激活就不会停止,我需要一种方法来完全停止这个功能及其所有内容和事件
line.addEventListener('click', function () {
$(function(){
var canvasOffset=$("#canvas").offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
window.alert(offsetX);
var startX;
var startY;
var isDown=false;
var canvas2 = $("#canvas");
function drawLine(toX,toY,context){
context.beginPath();
context.moveTo(startX, startY);
context.lineTo(toX,toY);
context.stroke();
}
function handleMouseDown(e){
e.preventDefault();
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
// save drag-startXY,
// move temp canvas over main canvas,
// set dragging flag
startX=mouseX;
startY=mouseY;
contextTemp.clearRect(0,0,canvasTemp.width,canvasTemp.height);
$("#canvasTemp").css({ left:0, top:0 });
isDown=true;
}
function handleMouseUp(e){
e.preventDefault();
if(!isDown){return;}
// clear dragging flag
// move temp canvas offscreen
// draw the user's line on the main canvas
isDown=false;
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
drawLine(mouseX,mouseY,context);
}
function handleMouseMove(e){
e.preventDefault();
if(!isDown){return;}
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
// clear the temp canvas
// on temp canvas draw a line from drag-start to mouseXY
contextTemp.clearRect(0,0,canvasTemp.width,canvasTemp.height);
drawLine(mouseX,mouseY,contextTemp);
}
canvas2.mousedown(function(e){handleMouseDown(e);});
canvas2.mousemove(function(e){handleMouseMove(e);});
canvas2.mouseup(function(e){handleMouseUp(e);});
canvas2.mouseout(function(e){handleMouseUp(e);});
}); // end $(function(){});
});
此功能需要停止在变量点击上运行,例如停止按钮
但它无法阻止所有其他正在运行的功能
答案 0 :(得分:1)
为什么不解开你在第一时间设置的事件,因为你已经使用了jQuery,你可以做sg。像:
var handleMouseDown = function(e) {
/* ... */
};
// bind the event:
$( "#canvas" ).bind( "click", handler );
// unbind again:
$( "#canvas" ).unbind( "click", handler );
删除功能:
var fn = function() {
alert( 1 );
}
fn();
fn = undefined;
fn();