Javascript中定时器的使用方法

Javascript中定时器的使用方法

1.间隔定时器(每隔一段时间执行一次代码)

格式:setInterval(函数,时间) //时间单位是毫秒,每隔设置的时间执行函数里的内容一遍(一直执行)

//每隔一秒钟,输出一次i,并加一
var i=0
setInterval(function test(){
console.log(i)
i++
}, 1000)

2.延时定时器(规定时间执行代码)

格式:setTimeout(函数,时间) //到达相应是时间执行代码(只执行一遍)

//五秒钟后执行输出语句
setTimeout(function(){
console.log("炸弹定时器,到到达时间后执行")
},5000)

3.关闭定时器和定时器返回值

//定时器返回值:第几个定时器(数值型)
var  num1=setTimeout(函数,时间)
var num2=setInterval(函数,时间)   

//关闭定时器格式(两个都多可以使用)
clearTimeout(num1)
clearInterval(num2)
posted @   永恒之月TEL  阅读(637)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开
  1. 1 Welcome Home REOL
  2. 2 Everywhere We Go REOL
  3. 3 凄美地 REOL
  4. 4 最后一页 REOL
Welcome Home - REOL
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Benjamin Cooper

作曲 : Benjamin Cooper

Sleep don't visit, so I choke on sun

And the days blur into one

And the backs of my eyes hum with things I've never done

Sheets are swaying from an old clothesline

[01:08.0]Like a row of captured ghosts over old dead grass

Was never much but we made the most

Was never much but we made the most

Welcome home

Ships are launching from my chest

Ships are launching from my chest

Some have names but most do not

Some have names but most do not

you find If one, please let me know what piece I've lost

Heal the scars from off my back

I don't need them anymore

I don't need them anymore

You can throw them out or keep them in your mason jars

I've come home

I've come home

All my nightmares escaped my head

All my nightmares escaped my head

Bar the door, please don't let them in

Bar the door, please don't let them in

You were never supposed to leave

You were never supposed to leave

Now my head's splitting at the seams

Now my head's splitting at the seams

And I don't know if I can

And I don't know if I can

Here, beneath my lungs

Here, beneath my lungs

I feel your thumbs press into my skin again