2 3 定時器和時間物件

2021-09-27 13:47:07 字數 3663 閱讀 5889

一、定時器分為兩種

// 開啟定時器

// setinterval(function() {}, 時間) 返回定時器id

// settimeout(function() {}, 超時時間) 返回定時器id

//清楚定時器

// cleartimeout(定時器id)

// clearinterval(定時器的id)

傳送驗證碼<

/button>

var btn = document.

queryselector

('button'

)var timer =

null

var n =

10 btn.onclick =

function()

},1000)}

<

/script>

定時器裡面的this 指的是window

二、時間物件

//時間物件

//new date() //建立此時此刻的時間物件 是自己電腦上的時間

var date =

newdate()

//thu oct 03 2019 14:21:01 gmt+0800 (中國標準時間)

console.

log(date)

//獲取年

var year = date.

getfullyear()

console.

log(year)

//獲取月 0-11 對應的 1-12月

var month = date.

getmonth()

+1console.

log(month)

//獲取日

var day = date.

getdate()

console.

log(day)

//獲取星期 0-6 對應的是 日-六

var week = date.

getday()

console.

log(week)

//獲取小時 只是當前的秒數

var h = date.

gethours()

console.

log(h)

//獲取分鐘

var m = date.

getminutes()

console.

log(m)

//獲取秒

var s = date.

getseconds()

console.

log(s)

// 案例:

//時間顯示器

var hhh = document.

queryselector

('h2'

)var str =

'日一二三四五六'

//通過下標星期0-6 對應的 日-六

hhh.innerhtml =

'現在的時間是:'

+ year +

'年'+ month +

'月'+ day +

'日'+

'星期'

+ str[week]

+totwo

(h)+

':'+

totwo

(m)+

':'+

totwo

(s)setinterval

(function()

,1000

)

時間物件的傳參

// new date()//當前時間

var d =

newdate()

// 怎麼獲取本地時間呢

console.

log(d.

tolocalestring()

)//2019/10/3 下午4:33:52

//new date() 物件傳入引數

var c =

newdate

(2019,11

,12,12

,12,12

,12)console.

log(c)

//thu dec 12 2019 12:12:12 gmt+0800 (中國標準時間)

console.

log(c.

tolocalestring()

)//2019 / 12 / 12 下午12: 12: 12

console.

log(c.

tostring()

)// 轉標準時間thu dec 12 2019 12:12:12 gmt+0800 (中國標準時間)

// 不傳時分秒

var f =

newdate

(2019,8

,12)console.

log(f.

tolocalestring()

)//2019/9/12 上午12:00:00

// 設定年月日 時分秒

var g =

newdate

(2019,8

,12)g.

setfullyear

(2020

)//同理 年月日時分秒 都可以設定

g.setmonth(20

)// 有自動容錯能力

console.

log(g.

tolocalestring()

)//2021 / 9 / 12 上午12: 00: 00

時間戳

距離搶購結束還有20天8小時20分鐘30秒<

/h2>

var hh = document.

queryselector

('h2'

)var timer =

null

var row =

newdate

(2019,9

,3,17

,48)//未來的時間物件 這個時間自己調節

timer =

setinterval

(fn,

1000)fn

()//防止頁面重新整理 顯示原始值

function fn()

var d = math.

floor

(s /

86400

)//天數

var h = math.

floor

((s %

86400)/

3600

)//小時

var m = math.

floor((

(s %

86400)%

3600)/

60)//分鐘數

var ms = math.

floor((

(s %

86400)%

3600)%

60)//秒數s

hh.innerhtml =

'距離搶購結束還有'

+ d +

'天'+ h +

'小時'

+ m +

'分鐘'

+ ms +

'秒'}

<

/script>

關於 時間和定時器

這些東西 本來感覺沒有什麼搞得必要 但是 感覺很多書 都介紹了一些 我也就跟著做一下吧。獲取系統的時間 這個 應該是是最簡單的 直接看 就ok include stdafx.h include int tmain int argc,tchar ar 關於 systemtime 這個結構體 可以直接看...

日期物件和定時器

一 gmt 0800是中國時間,比標準格林尼治時間快8個小時,1970.1.1 0 00分為格林尼治標準時間 二 例項化日期物件 當前時間 電腦上的當前時間 var date new date 三 日期建立的方式 1 無參建立 當前時間 var date new date 2 數字單參 毫秒數 va...

linux時間和定時器zz

定時函式,用於讓程式等待一段時間或安排計畫任務 一般情況下,獲取當前時間常用gettimerofday,因為它的精度是1us,並且在x86平台上它是使用者態實現的,沒有系統呼叫和上下文切換的開銷。定時函式中 sleep alarm在實現時有可能用了訊號 sigalrm,在多執行緒程式中處理訊號是個相...