求大神把js计时功能单独抽离出来

JavaScript011

求大神把js计时功能单独抽离出来,第1张

<html>

<meta http-equiv="Content-Type" content="text/html charset=utf-8" />

<div id="messages">

亲爱的,这是我们相爱在一起的时光。

<div id="elapseClock"></div>

</div>

<script>

function timeElapse(date){

var current = Date()

var seconds = (Date.parse(current) - Date.parse(date)) / 1000

var days = Math.floor(seconds / (3600 * 24))

seconds = seconds % (3600 * 24)

var hours = Math.floor(seconds / 3600)

if (hours < 10) {

hours = "0" + hours

}

seconds = seconds % 3600

var minutes = Math.floor(seconds / 60)

if (minutes < 10) {

minutes = "0" + minutes

}

seconds = seconds % 60

if (seconds < 10) {

seconds = "0" + seconds

}

var result = "<span class=\"digit\">" + days + "</span> days <span class=\"digit\">" + hours + "</span> hours <span class=\"digit\">" + minutes + "</span> minutes <span class=\"digit\">" + seconds + "</span> seconds" 

document.getElementById("elapseClock").innerHTML = result

}

var together = new Date()

together.setFullYear(2013, 2, 28)

together.setHours(20)

together.setMinutes(0)

together.setSeconds(0)

together.setMilliseconds(0)

timeElapse(together)

setInterval(function () {

timeElapse(together)

}, 500)

</script>

</html>

这个是js的计时器功能,不需要用到jquery的,你把代码复制新建一个html文件把代码放进去在浏览器打开就可以了

1、js中函数的封装的原理是把用户需要操作的业务抽离出来给用户操作。毕竟用户需求千变万化。

2、对于js函数的封装需要理解js函数的形式参数,实际参数与不定参数(arguments)。

3:下面是一个简单的以ID获取元素的方法

function $id(id){

return document.getElementById(id)

}

$id("box")//调用函数

4:形式参数的命名与Js中变量的命名规则一样

5:下面的图片是一个简单的函数封装参数传递示意图

不太明白你提的问题。Jquery就需要引入Jquery的一个.js文件,然后你自己写的.js文件如果里面有用到jquery的话,就应该放在Jquery愿文件的下面:如有前后关系。不知你时候遇到这个问题