猿问

如下的监听函数怎么传参

(color_array[i])是函数外的值(会更新)。如下的监听触发,不会改变(color_array[i]),
varcanvas=document.getElementById("mycanvas");
varctx=canvas.getContext("2d");
//console.log(ctx);
canvas.width=canvas.height=200;
ctx.beginPath();
varscore=0,time=60,flag=0;//定义初始值,时间
varcolor_array=["red","white","black","blue","green","DarkSlateGray3","DarkSlateGray","DarkOliveGreen4"];
ctx.moveTo(0,0);
ctx.lineTo(100,0);
ctx.lineTo(50,50);
vargameState=setInterval(changeColor,1000);
functionchangeColor(){
if(!time){
clearInterval(gameState);
}
vardomTime=document.querySelector(".time"),domScore=document.querySelector(".score");
domTime.innerHTML=time;//插入时间
vari=time%8;//根据time值顺序遍历数组
//触摸监听
canvas.addEventListener("touchstart",
function(){
if(color_array[i]==='white'){
console.log(i);
flag=1;//设置状态,注意这里有多次触发
}
}
,false);
if(flag==1){
score++;
domScore.innerHTML=score;
flag=0;//清楚状态,重新累加积分
}
ctx.fillStyle=color_array[i];//changecolor
ctx.fill();
i++;
time--;
}
函数为什么console.log(i)一直没不变。。我想过传外面的参,不过怎么想都不知道
墨色风雨
浏览 240回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答