settimeout提示函数没有定义

//时间
	   var endtime = '3600';
	   var Account = '';
	   RemainTimes();
	   function RemainTimes(){
           var iHour,iMinute,iSecond;
           var sHour="",sMinute="",sSecond="",sTime="";
           if (endtime >= 0){
              
               iHour = parseInt((endtime/3600)%24);
               if (iHour > 0){
                   sHour = iHour + "小时";
               }
               iMinute = parseInt((endtime/60)%60);
               if (iMinute > 0){
                   sMinute = iMinute + "分钟";
               }
               iSecond = parseInt(endtime%60);
               if (iSecond >= 0){
                   sSecond = iSecond + "秒";
               }
               
               //到点警告
               if (sHour==""){
               }
               else{
                   
               }
               
               sTime=sHour+sMinute+sSecond;
               if(endtime==0){
                   clearTimeout(Account);
                     sTime="时间到了!";
               }else{
            	   Account = setTimeout("RemainTimes()",1000);
               }
               endtime=endtime-1;
           } else{
               sTime="倒计时结束!";
           }
           $("#remaintime").text(sTime);
       }


这个是原来的代码,会提示RemainTimes没有定义,解决办法就是不要直接在settimeout中写函数,而是使用匿名函数的方法


 //时间
	   var endtime = '3600';
	   var Account = '';
	   RemainTimes();
	   function RemainTimes(){
           var iHour,iMinute,iSecond;
           var sHour="",sMinute="",sSecond="",sTime="";
           if (endtime >= 0){
              
               iHour = parseInt((endtime/3600)%24);
               if (iHour > 0){
                   sHour = iHour + "小时";
               }
               iMinute = parseInt((endtime/60)%60);
               if (iMinute > 0){
                   sMinute = iMinute + "分钟";
               }
               iSecond = parseInt(endtime%60);
               if (iSecond >= 0){
                   sSecond = iSecond + "秒";
               }
               
               //到点警告
               if (sHour==""){
               }
               else{
                   
               }
               
               sTime=sHour+sMinute+sSecond;
               if(endtime==0){
                   clearTimeout(Account);
                     sTime="时间到了!";
               }else{
            	   Account = setTimeout(function(){RemainTimes();},1000);
               }
               endtime=endtime-1;
           } else{
               sTime="倒计时结束!";
           }
           $("#remaintime").text(sTime);
       }


你可能感兴趣的:(settimeout)