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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>js实现倒计时60秒的简单代码(推荐)</title>
</head>
 <style type="text/css">
     body{
            margin: 200px auto;
            width: 200px;
            height: 200px;
     }
     #btn{
        width: 200px;
        height: 200px;
        background: black;
        color: white;
        font-size: 25px;
        -moz-border-radius: 100px;
        -webkit-border-radius: 100px;
        border-radius: 100px;
     }
 </style>
<body>
<input type="button" id="btn" value="开始倒计时" onclick="settime(this)" />
<script type="text/javascript">
    var countdown=getParam('time')?getParam('time'):60;
    function settime(val) {
        if (countdown == 0) {
            val.removeAttribute("disabled");
            val.value="开始倒计时";
            countdown = getParam('time')?getParam('time'):60;
        } else {
            val.setAttribute("disabled", true);
            val.value="倒计时(" + countdown + ")";
            countdown--;
            setTimeout(function() {
                settime(val)
            },1000)
        }
  
    }
 
    /** 
     * 获取指定的URL参数值 
     * URL:http://www.quwan.com/index?name=tyler 
     * 参数:paramName URL参数 
     * 调用方法:getParam("name") 
     * 返回值:tyler 
     */
    function getParam(paramName) { 
        paramValue = "", isFound = !1; 
        if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) { 
            arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0; 
            while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++ 
        } 
        return paramValue == "" && (paramValue = null), paramValue 
    } 
</script>
</body>
</html>

效果如下:


by 雪洁 2018-12-27 11:07:26 105 views
我来说几句

相关文章