首页 > jquery验证码倒计思路问题

jquery验证码倒计思路问题

http://jsbin.com/xoqizaxecu/edit?html,css,js,output

$(".huoqu").on("click",function(){
 var $this=$(this);
$this.addClass("gray");
var wait=10;
$this.html(wait+"s后获取");
  function fstime(){
    if(wait==0){
      $(".huoqu").removeClass("gray");
      $(".huoqu").text("获取验证码");
      wait =10;
    }else {
      console.log(wait);
        $this.addClass("gray");
      $(".gray").html(wait+"s后获取");
      wait--;
    }
  }
  setInterval(fstime,1000);
});

<span class="huoqu">获取验证码</span>

为什么wait还在无限循环?
console.log 控制台里面 0之后又重新继续计算?
验证码逻辑我问题出在哪句?
如何实现验证码业务问题?


应该等wait等于0时 clearInterval() 你还给wait 赋值10干啥呢


你的代码用了setInterval,就是一个无限执行的语句。

而且没有停止它,当然会一直执行下去喽


你没清除定时器clearInterval

var time ;
$(".huoqu").on("click",function(){
 var $this=$(this);
$this.addClass("gray");
var wait=10;
$this.html(wait+"s后获取");
  function fstime(){
    if(wait==0){
      clearInterval(time);
      $(".huoqu").removeClass("gray");
      $(".huoqu").text("获取验证码");
      wait =10;
    }else {
      console.log(wait);
        $this.addClass("gray");
      $(".gray").html(wait+"s后获取");
      wait--;
    }
  }
  time = setInterval(fstime,1000);
});
【热门文章】
【热门文章】