我这里用一个id为countdown的隐藏input标签输入倒计时时长(value值,单位秒),用id分别为hour、mini、sec的三个标签记录时、分、秒
html部分如下:
<input type="hidden" id="countdown" value="1000000" />
剩余时间:<span id="hour">11</span>小时<span id="mini">11</span>分<span id="sec">11</span>秒
jq部分如下(记得载入jq库):
<script type="text/javascript">
function function_exists(a){if(typeof a=='string'){return(typeof window[a]=='function')}else{return(a instanceof Function)}}
var countdown = function(id, total, callback) {
if (total <= 0) {
function_exists(callback) &&callback.call(callback)
return
}
var start_hours = parseInt(total / (60 * 60 * 1000), 10)
var start_minutes = parseInt((total - start_hours * 60 * 60 * 1000) / (60 * 1000), 10)
var start_sec = parseInt(((total - start_hours * 60 * 60 * 1000) - start_minutes * 60 * 1000) / 1000)
$("#sec").html(start_sec <10 ? '0' + start_sec: start_sec)
$("#mini").html(start_minutes <10 ? '0' + start_minutes: start_minutes)
$("#hour").html(start_hours <10 ? '0' + start_hours: start_hours)
total = total - 100
var self = this
setTimeout(function() {
self.countdown(id, total, callback)
},
100)
}
var obj = {
sec: $("#sec"),
mini: $("#mini"),
hour: $("#hour")
}
function run_countdown()
{
if( $('#countdown').size() )
{
countdown(obj, parseInt($('#countdown').val() * 1000), function()
{
$("#sec").html('00')
$("#mini").html('00')
$("#hour").html('00')
})
}
}
$(function(){
run_countdown()
})
</script>
倒计时可以用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>
var t=60var a=setInterval(daojishi,1000)//1000毫秒
function daojishi(){
t--
//刷新时间显示
if(t==0){
clearInterval(a)
//倒计时结束
}
}