jquery实现60秒倒计时

<html>
<head>
    <title>123456</title>
    <script type="text/javascript" src="/js/jquery-1.11.3/jquery.min.js"></script>
    <script>
    var time = 60;
    $(function(){
        $("#sendsms").click(function (){
    	   if(time==60){//如果不加入该判断,则会出现在倒计时期间不断的点击发生不断的加快(其实就是你点了多少次就重复多少次该函数)的问题,用setTimeout()方法不加此判断也是一样的
    		   var time1=setInterval(function(){
        		   if(time==0){
            		   $("#sendsms").html("重新发送");
            		   $("#sendsms").removeAttr("disabled");
            		   time=60;
            		   clearInterval(time1);
            	   }else{
            		   $("#sendsms").attr("disabled","true");
            		   $("#sendsms").html("重新发送("+time+")");
            		   time--;
            	   }
        		   }, 1000);
    	   }
    	   
    	   })
    })
    </script>
</head>
</html>
<body>
<a id="sendsms" href="javascript:void(0)">发送短信</a>
</body>

第一次发帖,不会编辑。。。。。

注意的是,a标签href属性值为javasrcipt:void(0)或javasrcipt:;,这样用可以避免页面刷新、跳转,很多网站都这样用,如某宝,点击的时候看浏览器左下角的响应会有显示的。

猜你喜欢

转载自blog.csdn.net/weixin_43168278/article/details/88644330