设置延时执行

当设置多个延时执行时,最好用一个数组把它们定义,举例如下
stop=0;
	timename=new Array();
	for(j=1;j<21;j++){
		a=j%20;
		switch(a){
		case 1: 
		timename[0]=setTimeout("if(!stop){showEarth(1996);}",0);
		break;
		case 2: 
		//showEarth(1997);
		timename[1]=setTimeout("if(!stop){showEarth(1997);}",intervaltime);
		break;
		case 3: 
		//showEarth(1998);
		timename[2]=setTimeout("if(!stop){showEarth(1998);}",2*intervaltime);
		break;
		case 4: 
		//showEarth(1999);
		timename[3]=setTimeout("if(!stop){showEarth(1999);}",3*intervaltime);
		break;
		case 5: 
		//showEarth(2000);
		timename[4]=setTimeout("if(!stop){showEarth(2000);}",4*intervaltime);
		break;
		case 6: 
		//showEarth(2001);
		timename[5]=setTimeout("if(!stop){showEarth(2001);}",5*intervaltime);
		break;
		case 7: 
		//showEarth(2002);
		timename[6]=setTimeout("if(!stop){showEarth(2002);}",6*intervaltime);
		break;
		case 8:
		// showEarth(2003);
		timename[7]=setTimeout("if(!stop){showEarth(2003);}",7*intervaltime);
		break;
		case 9: 
		//showEarth(2004);
		timename[8]=setTimeout("if(!stop){showEarth(2004);}",8*intervaltime);
		break;
		case 10: 
		//showEarth(2005);
		timename[9]=setTimeout("if(!stop){showEarth(2005);}",9*intervaltime);
		break;
		case 11:
		// showEarth(2006);
		timename[10]=setTimeout("if(!stop){showEarth(2006);}",10*intervaltime);
		break;
		case 12:
		// showEarth(2007);
		timename[11]=setTimeout("if(!stop){showEarth(2007);}",11*intervaltime);
		break;
		case 13: 
		//showEarth(2008);
		timename[12]=setTimeout("if(!stop){showEarth(2008);}",12*intervaltime);
		break;
		case 14: 
		//showEarth(2009);
		timename[13]=setTimeout("if(!stop){showEarth(2009);}",13*intervaltime);
		break;
		case 15: 
		//showEarth(2010);
		timename[14]=setTimeout("if(!stop){showEarth(2010);}",14*intervaltime);
		break;
		case 16: 
		//showEarth(2011);
		timename[15]=setTimeout("if(!stop){showEarth(2011);}",15*intervaltime);
		break;
		case 17:
		// showEarth(2012);
		timename[16]=setTimeout("if(!stop){showEarth(2012);}",16*intervaltime);
		break;
		case 18: 
		//showEarth(2013);
		timename[17]=setTimeout("if(!stop){showEarth(2013);}",17*intervaltime);
		break;
		case 19: 
		//showEarth(2014);
		timename[18]=setTimeout("if(!stop){showEarth(2014);}",18*intervaltime);
		break;
		case 0: 
		//showEarth(2015);
		timename[19]=setTimeout("if(!stop){showEarth(2015);}",19*intervaltime);
		break;
		default: break;
		}
	}

这里有20个延时执行的代码,如果我要在执行到它们其中一个时让他们不再执行下去,应该怎么办呢(ps上面的代码有bug,明明可以直接写这二十行延时命令,不需要for和switch)

就需要把它们所有的都clearTimeout,代码如下

clearTimeout(timename[0]);
   clearTimeout(timename[1]);
   clearTimeout(timename[2]);
   clearTimeout(timename[3]);
   clearTimeout(timename[4]);
   clearTimeout(timename[5]);
   clearTimeout(timename[6]);
   clearTimeout(timename[7]);
   clearTimeout(timename[8]);
   clearTimeout(timename[9]);
   clearTimeout(timename[10]);
   clearTimeout(timename[11]);
   clearTimeout(timename[12]);
   clearTimeout(timename[13]);
   clearTimeout(timename[14]);
   clearTimeout(timename[15]);
   clearTimeout(timename[16]);
   clearTimeout(timename[17]);
   clearTimeout(timename[18]);
   clearTimeout(timename[19]);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值