繁体   English   中英

多次点击操作-Javascript

[英]Multiple click actions - Javascript

我有一个ID为'draw'的按钮,并且想从中获取,每次我单击该绘制按钮时,都要进行下一个要处理的操作(即我的形状)。 我尝试了style.visibility,但是没有用。 也许我做错了。 但是,这是代码:

window.onload = function() {
  var canvas = document.getElementById('myCanvas'), 
      draw = document.getElementById('draw'),
      clr = document.getElementById('clear'), 
      context = canvas.getContext('2d');
  var grad = context.createLinearGradient(100,0,200,0);

function shapeOne(){
    context.beginPath(); // circle
    context.fillStyle = 'blue';
    context.arc(200, 100, 100, 50 , 2 * Math.PI, false);
    context.fill();
    context.lineWidth = '1';
    context.stroke();
    context.closePath(); 
  }


function shapeTwo(){
    context.beginPath(); // rectangle
    grad.addColorStop(0,"#0033cc");
    grad.addColorStop(1,"#0066ff");
    context.fillStyle=grad;
    context.fillRect(120,40,160,120);
    context.strokeStyle="black";
    context.strokeRect(120,40,160,120);
    context.closePath();
  }

function shapeThree(){
    context.beginPath(); // line
    context.moveTo(280, 40);
    context.lineTo(120, 160);
    context.stroke();
    context.closePath();
  }

  draw.addEventListener('click', shapeOne, false);      
  draw.addEventListener('click', shapeTwo, false);
  draw.addEventListener('click', shapeThree, false);

  clr.addEventListener('click', function(){
    canvas.width = canvas.width;
  }, false);  

};

正如其他人所说,当前绑定到“ draw”元素的所有事件侦听器将同时触发。 确实不需要多个事件侦听器。 您只需要知道当前要绘制什么形状的方法即可。

因此,我将声明一个名为currentShape类的变量并将其设置为零。

var canvas = document.getElementById('myCanvas'), 
  draw = document.getElementById('draw'),
  clr = document.getElementById('clear'), 
  context = canvas.getContext('2d');
var grad = context.createLinearGradient(100,0,200,0);
// Start current shape at 0
var currentShape = 0;
// More code ...

然后,我将创建一个数组,该数组基本上是您拥有的形状绘制函数的列表。

像这样:

var shapes = [shapeOne, shapeTwo, shapeThree]

然后,不要使用三个事件侦听器,而是使用一个看起来像这样的事件侦听器:

draw.addEventListener('click', function(){
    // Check if there is a function available for the current shape.
    if(shapes[currentShape]) {
        // If so, call that function and add one to the current shape.
        shapes[currentShape]();
        currentShape += 1;
    } else { // If the function isn't available, then lets assume we have already drawn all the shapes and need to start again.
         currentShape = 0;
         shapes[currentShape]();
    }
}, false); 

您还可以通过许多其他方式来执行此操作。 包括使用switch语句 ,这可能会删除一些代码重复。

不,这不是这样做的方法 ,因为当您将多个侦听器添加到html元素的同一事件中时,所有侦听器将同时触发。

因此,您需要创建一个poll(函数数组) ,然后在单击元素时将一个新函数放入一个数组中执行,然后另一个函数控制这些函数的执行

您可以使用一个函数数组,您的click事件会将函数放在该数组中,然后您可以在另一时间调用(如在stackoverflow问题上所说明的)。

如果要控制功能列表的 动态执行,此示例是更好的解决方案

如何添加功能列表:

var listRuns = [],
    ind = 0;
$('#btnRun').click(function(){
  var message = document.getElementById("txtMsg").value;

  listRuns.push(function(){
    run(message);
  });
});

如何执行它们:

var runAll = function(){  
  console.log('called');
  if (listRuns.length>ind){
    listRuns[ind]();
    ind++; 
  }
};
var idInterval = setInterval(function(){
  runAll();
},1000);

因此,与用户单击多少无关,您将执行所有这些操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM