JS內建物件Date 寫倒計時

2022-09-02 18:51:07 字數 525 閱讀 5396

function

countdown(time)

console.log(countdown('2020-04-10 23:30:00'));

核心演算法:

1.輸入的時間減去現在的時間就是剩餘的時間,即倒計時,但是不能拿著時分秒相減,比如05分減去25分,結果會是負數

2.用時間戳來做,使用者輸入時間總的毫秒數減去現在時間的總的毫秒數,得到的就是剩餘時間的毫秒數

3.把剩餘時間總的毫秒數轉換為天、時、分秒(時間戳轉換為天時分秒)

秒數轉換公式:

d = parseint(times / 60 / 60 / 24);

h = parseint(times / 60 / 60 % 24);

m = parseint(times / 60 % 60);

s = parseint(times % 60);

JS Math方法 Date物件 倒計時

math.floor 向下取整 取小 math.ceil 向上取整 取大 math.round 四捨五入 負數同理 math.random 隨機數 0,1 演算法 取兩個數的隨機整數 min,max math.floor math.random max min 1 min new array 物件 ...

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 ...