图上的倒计时怎么用js写

JavaScript031

图上的倒计时怎么用js写,第1张

首先把你所剩余的时间算好,然后使用setInterval函数时间定为1000毫秒,然后每过一秒减1秒,

如果秒数减到0的时候就减分钟,如果分钟减到零了,那时间就是00:00:00啦

<script language="JavaScript">

function setTab(name,cursel,n){

for(i=1i<=ni++){

var menu=document.getElementById(name+i)

var con=document.getElementById("con_"+name+"_"+i)

menu.className=i==cursel?"hover":""

con.style.display=i==cursel?"block":"none"

}

}var DifferenceHour = -1

var DifferenceMinute = -1

var DifferenceSecond = -1

var Tday = new Date("Feb 14, 2010 00:00:00") //**倒计时时间点-注意格式

var daysms = 24 * 60 * 60 * 1000

var hoursms = 60 * 60 * 1000

var Secondms = 60 * 1000

var microsecond = 1000function left(mainStr,lngLen) {

if (lngLen>0) {return mainStr.substring(0,lngLen)}

else{return null}

}

function right(mainStr,lngLen) { if (mainStr.length-lngLen>=0 &&mainStr.length>=0 &&mainStr.length-lngLen<=mainStr.length) {

return mainStr.substring(mainStr.length-lngLen,mainStr.length)}

else{return null}

}

function mid(mainStr,starnum,endnum){

if (mainStr.length>=0){

return mainStr.substr(starnum,endnum)

}else{return null} }function clock()

{

var time = new Date()

var hour = time.getHours()

var minute = time.getMinutes()

var second = time.getSeconds()

var timevalue = ""+((hour >12) ? hour-12:hour)

timevalue +=((minute <10) ? ":0":":")+minute

timevalue +=((second <10) ? ":0":":")+second

timevalue +=((hour >12 ) ? " PM":" AM")var convertHour = DifferenceHour

var convertMinute = DifferenceMinute

var convertSecond = DifferenceSecond

var Diffms = Tday.getTime() - time.getTime()

DifferenceHour = Math.floor(Diffms / daysms)

Diffms -= DifferenceHour * daysms

DifferenceMinute = Math.floor(Diffms / hoursms)

Diffms -= DifferenceMinute * hoursms

DifferenceSecond = Math.floor(Diffms / Secondms)

Diffms -= DifferenceSecond * Secondms

var dSecs = Math.floor(Diffms / microsecond)var dd=document.getElementById("dd")

var hh=document.getElementById("hh")

var mm=document.getElementById("mm")

var ss=document.getElementById("ss")ddstr = DifferenceHour.toString()

hhstr = DifferenceMinute.toString()

mmstr = DifferenceSecond.toString()

ssstr = dSecs.toString()if (Tday.getTime() >time.getTime()){

if (ddstr.length<2){

ddstrs = "<span class='num0'></span><span class='num"+ddstr.substr(0,1)+"'></span>"

}else{

ddstrs = "<span class='num"+ddstr.substr(0,1)+"'></span><span class='num"+ddstr.substr(1,2)+"'></span>"

}if (hhstr.length<2){

hhstrs = "<span class='num0'></span><span class='num"+hhstr.substr(0,1)+"'></span>"

}else{

hhstrs = "<span class='num"+hhstr.substr(0,1)+"'></span><span class='num"+hhstr.substr(1,2)+"'></span>"

}if (mmstr.length<2){

mmstrs = "<span class='num0'></span><span class='num"+mmstr.substr(0,1)+"'></span>"

}else{

mmstrs = "<span class='num"+mmstr.substr(0,1)+"'></span><span class='num"+mmstr.substr(1,2)+"'></span>"

}if (ssstr.length<2){

ssstrs = "<span class='num0'></span><span class='num"+mmstr.substr(0,1)+"'></span>"

}else{

ssstrs = "<span class='num"+ssstr.substr(0,1)+"'></span><span class='num"+ssstr.substr(1,2)+"'></span>"

}

}else{

ddstrs="<span class='num0'></span><span class='num0'></span>"

hhstrs="<span class='num0'></span><span class='num0'></span>"

mmstrs="<span class='num0'></span><span class='num0'></span>"

ssstrs="<span class='num0'></span><span class='num0'></span>"

}if(convertHour != DifferenceHour) dd.innerHTML=ddstrs

if(convertMinute != DifferenceMinute) hh.innerHTML=hhstrs

if(convertSecond != DifferenceSecond) mm.innerHTML=mmstrs

ss.innerHTML=ssstrs

setTimeout("clock()",1000)}</script>上面的是JS部分的。 下面是HTML部分的。<div class="mains">

<div class="djs">

<div id="dd"></div>

<div id="tsfm">天</div>

<div id="hh"></div>

<div id="tsfm">时</div>

<div id="mm"></div>

<div id="tsfm">分</div>

<div id="ss"></div>

<div id="tsfm">秒</div>

</div>我上面的JS部分的数字全部是用的图片格式显示倒计时数字的哈,你也可以自己改成数字。 好了,上面的代码全部是我之前给客户写的一个春节倒计时贺卡的程序页面,纯原创,并且一直在用,所以是没有任何问题的,你自己应该懂一些技术吧,所以上面的代码你应该看的懂,很简单的。 就不要再追问了,自己多下下功夫研究下,因为最近比较忙,不见得有时间回复,见谅。记得给我分哈,要升级这个号的级别,以后好推广用,谢谢了。

倒计时可以用js的setTimeout来控制

http://www.tocus.com.cn/?send=article_show&id=34&class=2

可以循环计时,而对于页面刷新,我们可以屏蔽鼠标右键、Ctrl+N、Shift+F10、Alt+F4、F11、F5刷新、退格键来达到效果

<script>

//屏蔽鼠标右键、Ctrl+N、Shift+F10、F11、F5刷新、退格键

function document.oncontextmenu(){event.returnValue=false}//屏蔽鼠标右键

function window.onhelp(){return false} //屏蔽F1帮助

function document.onkeydown(){

if((window.event.altKey)&&((window.event.keyCode==37)||(window.event.keyCode==39))){

//屏蔽Alt+方向键←

//屏蔽Alt+方向键→

event.returnValue=false

}

if((event.keyCode==8)||(event.keyCode==116)||(event.ctrlKey &&event.keyCode==82)){

//屏蔽退格删除键

//屏蔽F5刷新键

//Ctrl+R

event.keyCode=0

event.returnValue=false

}

if(event.keyCode==122){event.keyCode=0event.returnValue=false}//屏蔽F11

if(event.ctrlKey &&event.keyCode==78)event.returnValue=false //屏蔽Ctrl+n

if(event.shiftKey &&event.keyCode==121)event.returnValue=false //屏蔽shift+F10

if(window.event.srcElement.tagName=="A" &&window.event.shiftKey)

window.event.returnValue=false //屏蔽shift加鼠标左键新开一网页

if((window.event.altKey)&&(window.event.keyCode==115)){ //屏蔽Alt+F4

window.showModelessDialog("about:blank","","dialogWidth:1pxdialogheight:1px")

return false

}

}

</script>