星期五, 9月 18, 2009

[Javascript 茶包筆記] setTimeout 倒數計數

專案需用到倒數計數的功能,
以下是一個簡單的測試版本,
另外又在網上找到一個將秒數轉時分秒(TimeToHMS)
javascript func。

script範例程式如下:

//20分鐘
var allTime = 1200000;
$(document).ready(function(){
$("#myDiv").css("border","3px solid red");
DoTestCount();
});

function DoTestCount()
{
if(allTime <= 0){
//dopostback

__doPostBack(this.btnSubmit,"")
}
else
{
var timeTmp = allTime/1000;
var currentTimeHMS = TimeToHMS(timeTmp)
$("#myDiv").text(currentTimeHMS)
setTimeout("DoTestCount()",1000);
}
allTime-=1000;
}

/* convert seconds value to H:MM:SS format */
function TimeToHMS(seconds)
{
sec = seconds % 60;
temp = ( seconds - sec ) / 60;

minute = temp % 60;
hour = (temp - minute) / 60;

if(!(isFinite(sec) && isFinite(minute) && isFinite(hour))) /* invalid time */
{
return ("");
}

time_str = hour;
time_str += ":";
time_str+=(minute<10)?("0"+minute):minute;
time_str+=":";
time_str+=(sec<10)?("0"+sec):sec;

return (time_str);
}

網頁配置如下:

沒有留言:

張貼留言

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails