使用js倒計時還有幾天及計時過了幾天

2022-03-06 07:43:12 字數 2193 閱讀 7893

公司主營業務是體育賽事為主的;尤其是足球賽事。現正值2016法國歐洲盃;之前忙於做歐洲盃專題沒時間總結。現分享乙個工作中需求帶來的經歷。

需求是:從現在開始倒計時歐洲盃還有多久開賽;當開賽之後開始計算已經開賽了幾天及計算當前是小組賽還是1/8決賽,1/4決賽,半決賽,決賽。

廢話不多說,直接上**:

4

varcdinterval, dateinterval;

//此方法為倒計時歐洲盃開賽時間,原理是用開賽時間的毫秒數減去當前毫秒數然後根據每天,小時,分,秒的毫秒數向下取整

5function

countdown() else

15var nowtime = new

date(), distancetime

16 ,startserver = starttime.gettime(), nowserver =nowtime.gettime();

17 distancetime = startserver -nowserver;

1819

//一天的豪秒數86400000 乙個小時的毫秒數3600000 一分鐘的毫秒數 60000 一秒鐘的毫秒數 1000

20var

d_date, d_hour, d_min,d_second;

21if(distancetime > 0)

33if(d_hour < 10)

36if(d_min < 10)

39if(d_second < 10)

42 document.getelementsbyclassname('icon-time')[0].innerhtml = d_date;43 document.getelementsbyclassname('icon-time')[1].innerhtml = d_hour;44 document.getelementsbyclassname('icon-time')[2].innerhtml = d_min;45 document.getelementsbyclassname('icon-time')[3].innerhtml = d_second;46 } else

56});

57backwarddays()

58 dateinterval = setinterval(backwarddays, 10000);59}

60}61//

天數,原理為用當前毫秒數減去開賽時候的毫秒數計算出已經開賽了幾天

62function

backwarddays() else

112113

114var currenttime = new date().gettime();//

當前時間

115//

第幾天116 now_day = (currenttime - groupgamestart)/86400000;

117 dist_day = math.floor((currenttime - groupgamestart)/86400000);

118if(now_day >dist_day) else

123124

if(day === 0)

127if(day < 10)

130 $('.icon-time:eq(0)').text(day);

//根據時間範圍算出賽事範圍

131if(currenttime >= groupgamestart && currenttime <= groupgameend) else

if(currenttime >= eighthgamestart && currenttime <= eighthgameend) else

if(currenttime >= fourgamestart && currenttime <= fourgameend) else

if(currenttime >= halfgamestart && currenttime <= halfgameend) else

if(currenttime >= finalgame && currenttime <= endgame) else

144145

if(currenttime >=endgame)

148}

149 $(function

());

js倒計時,秒倒計時,天倒計時

html 1 html 2 距某某開幕式還有 天 html html html id timer style color red 這次利用系統時間自校驗倒計時,無需手工調校使得倒計時更為精確,及詳細注釋如下 id clock 00 01 11 00 id startb type button val...

js 多個倒計時,毫秒倒計時

其實主要是借鑑了了這篇文的寫法 俺稍作了修改,以便更適合我的需要 實現功能 呼叫乙個函式,傳入html元素的id,和乙個截止時間 unix時間戳 在該html元素中列印出到當前到截止時間為止的倒計時,精確到毫秒 效果圖如下 h2 毫秒的倒計時 h2 div id timer1 div div id ...

JS 簡易倒計時

簡易倒計時,非常簡單。這裡主要用到了setinterval 和clearinterval 方法 定義 setinterval 方法可按照指定的週期 以毫秒計 來呼叫函式或計算表示式。setinteral 方法會不停地呼叫函式,直到clearinterval 被呼叫或視窗被關閉。由setinterva...