10秒倒计时JS代码

JavaScript015

10秒倒计时JS代码,第1张

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script type="text/javascript">

<!--

var duration=9900

var endTime = new Date().getTime() + duration + 100

function interval()

{

var n=(endTime-new Date().getTime())/1000

if(n<0) return

document.getElementByIdx("timeout").innerHTML = n.toFixed(3)

setTimeout(interval, 10)

}

window.onload=function()

{

setTimeout("window.location.href='http://www.17mm.net'", duration)

interval()

}

//-->

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>等待10秒</title>

</head>

<body>

<form id="form1" runat="server">

<div>

现在剩下 <span id="timeout">10.000</span>秒后将自动跳转</div>

</form>

</body>

</html>

    //t 倒计时多少s  callback  倒计时完后要执行的function方法

        function djs(t, callback) {

            var ts = setInterval(function () { 

                                        t -= 1 

                                        if (t == 0) { 

                                            clearInterval(ts)

                                          callback(callback)

                                        }

              },1000)

        }

        //10s后弹出 xx

        $(function () {

            djs(10, function () {

                alert("xx")

            })

        })