Js实现:团购倒计时(不带天数)

JavaScript020

Js实现:团购倒计时(不带天数),第1张

方法很多的。

我这里用一个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>

<!DOCTYPE html>

<html lang="en">

<head>

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

<title>js实现倒计时60秒的简单代码(推荐)</title>

<script type="text/javascript" src="js/jquery.js"></script>

</head>

<body>

<input type="button" id="btn" value="免费获取验证码" onclick="settime(this)" />

<script type="text/javascript">

var countdown=60

function settime(val) {

if (countdown == 0) {

val.removeAttribute("disabled")

val.value="免费获取验证码"

countdown = 5

} else {

val.setAttribute("disabled", true)

val.value="重新发送(" + countdown + ")"

countdown--

setTimeout(function() {

settime(val)

},1000)

}

}

</script>

</body>

</html>

JavaScript简介

JavaScript 是脚本语言

JavaScript 是一种轻量级的编程语言。

JavaScript 是可插入 HTML 页面的编程代码。

JavaScript 插入 HTML 页面后,可由所有的现代浏览器执行。

JavaScript 课外书

如果 JavaScript 教程学习完毕,并且需要更深入地学习这门语言,《JavaScript 高级教程》绝对是您最好的选择。本教程从 JavaScript 的历史开始讲起,直到当前它对 XML 和 Web 服务的支持。

将学习到如何扩展该语言,以使它适应特殊的需求。

还将学到如何使用 JavaScript 创建无缝的客户机 - 服务器通信。

<SCRIPT LANGUAGE="JavaScript">

<!--

var maxtime = 60*60//60*60 //一个小时,按秒计算,自己调整!

function CountDown(){

if(maxtime>=0){

//minutes = Math.floor(maxtime/60)

seconds = Math.floor(maxtime%60)

//msg = "距离结束还有"+minutes+"分"+seconds+"秒"

msg = "距离结束还有"+seconds+"秒"

document.all["timer"].innerHTML=msg

if(maxtime == 5*60){

alert('注意,还有5分钟!')

}

--maxtime

}else{

clearInterval(timer)

//alert("时间到,结束!")

maxtime=60*60

timer = setInterval("CountDown()",1000)

}

}

timer = setInterval("CountDown()",1000)

//-->

</SCRIPT>

<div id="timer" style="color:red"></div>

<SCRIPT LANGUAGE="JavaScript">

<!--

var maxtime = 5*60//60*60 //一个小时,按秒计算,自己调整!

var timer

function CountDown(){

if(maxtime>=0){

var minutes = Math.floor(maxtime/60)

var seconds = Math.floor(maxtime%60)

var msg = "距离刷新还有"+minutes+"分"+seconds+"秒"

$('#timer').html(msg)

//document.all["timer"].innerHTML=msg

--maxtime

}else{

clearInterval(timer)

//一下两个方法是倒计时结束后调用的方法

searchTaskList()

myajax()

//设置下一次的倒计时

maxtime=5*60

timer = setInterval("CountDown()",1000)

}

}

timer = setInterval("CountDown()",1000)

//-->

</SCRIPT>

<div id="timer" style="display:inlinecolor:red"></div>