// Time var endtime = '20140901'; 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 + "Hour";} iMinute = parseInt (endtime/60) % 60); if (iMinute> 0) {sMinute = iMinute + "min";} iSecond = parseInt (endtime % 60); if (iSecond> = 0) {sSecond = iSeco Nd + "seconds" ;}// point-to-point warning if (sHour = "") {}else {}stime = sHour + sMinute + sSecond; if (endtime = 0) {clearTimeout (Account); sTime = "it's time! ";}Else {Account = setTimeout (" RemainTimes () ", 1000);} endtime = endtime-1;} else {sTime =" the countdown is over! ";}$ (" # Remaintime "). text (sTime );}
This is the original code and will prompt that the RemainTimes is not defined. The solution is not to write the function directly in settimeout, but to use the anonymous function method.
// Time var endtime = '20140901'; 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 + "Hour";} iMinute = parseInt (endtime/60) % 60); if (iMinute> 0) {sMinute = iMinute + "min";} iSecond = parseInt (endtime % 60); if (iSecond> = 0) {sSecond = iSeco Nd + "seconds" ;}// point-to-point warning if (sHour = "") {}else {}stime = sHour + sMinute + sSecond; if (endtime = 0) {clearTimeout (Account); sTime = "it's time! ";}Else {Account = setTimeout (function () {RemainTimes () ;}, 1000) ;}endtime = endtime-1 ;}else {sTime =" the countdown is over! ";}$ (" # Remaintime "). text (sTime );}