参考:https://blog.csdn.net/HolaOrange/article/details/123514382

视图页面:

按钮:

<%= link_to '获取验证码', 'javascript:void(0)', id:'send_validation_code', style: "#{style}; margin-left: -121px; position: absolute;display: inline-block; text-align: center; width: 120px; height: 55px; border-radius: 12px; padding: 15px 16px; box-sizing: border-box; font-size: 16px; outline: none; display: inline-block; " %>

js jquery:

<script>
  jQuery('#send_validation_code').click(function () {
    let email = jQuery('#email').val();
    let url =  '/front_ends/send_validation_code?type=email&to=' + email +'&account_id=<%= current_account.id %>'
    if(email==''){
      alert('邮箱不能为空')
    }else{
      /*发送前端验证码按钮变化*/
      if ($(this).hasClass('disabled')) {
      } else {
        var self = $(this);
        var count = 60;
        self.addClass('disabled');
        self.text(count + 's');
        // 定时器在这
        var timer = setInterval(function () {
          count--;
          if (count > 0) {
            self.text(count + 's');
          } else {
            clearInterval(timer);
            self.text('获取验证码');
            self.removeClass("disabled");
          }
        }, 1000);
        /*1秒= 1000ms*/
        jQuery.get(url , function(data){
          console.info(data)
          if(data.result == 'ok'){
            console.log("发送成功")
          }else{
            alert('该邮箱已经存在,无法被绑定,请确认邮箱是否正确')
          }
        })
      }
    }
  });
</script>

参考:

 //短信发送
    $('.vcode-send').click(function () {
        /*发送前端验证码按钮变化*/
        if ($(this).hasClass('disabled')) {
 
        } else {
            var self = $(this);
            var count = 10;
            self.addClass('disabled');
            self.text(count + '秒后重新获取');
 
// 定时器在这
            var timer = setInterval(function () {
                count--;
                if (count > 0) {
                    self.text(count + '秒后重新获取');
                } else {
                    clearInterval(timer);
                    self.text('重新获取验证码');
                    self.removeClass("disabled");
                }
            }, 1000);
            /*1秒= 1000ms*/
 
            /*后端搞验证码,确认成功*/
            var phone = $("#phone").val();
            $.get(domainUrl + "/users/sendVerifyCode", {phone:phone}, function (data) {
                /*后端: 造验证码,发短信,指定phone用Redis缓存code,返回Json成功!*/
                console.log(data);
                if(data.code === 200){
                    popup("发送成功")
                }else{
                    popup(data.msg);
                }
            })
        }
    });