最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue實現(xiàn)驗證碼按鈕倒計時功能

 更新時間:2018年04月10日 09:42:41   作者:菜鳥_先菲  
最近項目結(jié)束,空閑時間比較多,今天小編抽時間給大家使用vue寫一個小例子,就決定做驗證碼倒計時功能,具體實例代碼大家參考下本文

本人最近開始嘗試學(xué)習(xí)vue.js。想使用vue寫一個小例子,就選擇做驗證碼按鈕倒計時功能。

    上網(wǎng)上搜了一下,也把他們的代碼試了一下,自己出了很多問題。所以,需要寫一篇基礎(chǔ)入門的文章,避免后面人采坑。

   這是按照網(wǎng)上寫的HTML頁面

<div class="register-pannel" id ="register-pannel"> 
      <div class="register-l" align="center"> 
        <div class="input-group" style="width: 300px;"> 
          <input type="text" class="form-control" placeholder="郵箱/手機(jī)號/用戶名" style="height: 40px;" /> 
          <span class="glyphicon glyphicon-user form-control-feedback" aria-hidden="true" /> 
        </div> 
        <br /> 
        <div class="input-group" style="width: 300px;"> 
          <input type="text" class="form-control" placeholder="密碼" style="height: 40px;" /> 
          <span class="glyphicon glyphicon-lock form-control-feedback" /> 
        </div> 
        <br /> 
        <div class="input-group" style="width: 300px;"> 
          <input type="text" class="form-control" placeholder="手機(jī)號" style="height: 40px;" /> 
          <span class="glyphicon glyphicon-phone form-control-feedback" /> 
        </div> 
        <br /> 
        <div class="input-group" style="width: 300px;"> 
            <span class="register-msg-btn" v-show="show" v-on:click="getCode">發(fā)送驗證碼</span> 
            <span class="register-msg-btn" v-show="!show">{{count}} s</span> 
          <input type="text" class="form-control" placeholder="驗證碼" style="float: right; height: 40px; width: 150px;" /> 
          <span class="glyphicon glyphicon-font form-control-feedback" /> 
        </div> 
        <br /> 
        <span class="btn-register">注冊</span> 
      </div> 

js寫成

<script> 
<span style="white-space: pre;">      </span>data(){   
<span style="white-space: pre;">      </span>return {   
<span style="white-space: pre;">        </span>show: true,   
<span style="white-space: pre;">        </span>count: '',   
<span style="white-space: pre;">        </span>timer: null,   
<span style="white-space: pre;">      </span>}  
<span style="white-space: pre;">    </span>},  
<span style="white-space: pre;">    </span>methods:{   
<span style="white-space: pre;">      </span>getCode(){    
<span style="white-space: pre;">        </span>const TIME_COUNT = 60;    
<span style="white-space: pre;">        </span>if (!this.timer) {     
<span style="white-space: pre;">          </span>this.count = TIME_COUNT;     
<span style="white-space: pre;">          </span>this.show = false;     
<span style="white-space: pre;">          </span>this.timer = setInterval(() => {     
<span style="white-space: pre;">            </span>if (this.count > 0 && this.count <= TIME_COUNT) {      
<span style="white-space: pre;">              </span>this.count--;      
<span style="white-space: pre;">            </span>} else {      
<span style="white-space: pre;">              </span>this.show = true;      
<span style="white-space: pre;">              </span>clearInterval(this.timer);      
<span style="white-space: pre;">              </span>this.timer = null;      
<span style="white-space: pre;">            </span>}     
<span style="white-space: pre;">          </span>}, 1000)     
<span style="white-space: pre;">        </span>}   
<span style="white-space: pre;">      </span>}   
<span style="white-space: pre;">    </span>} 
</script> 

發(fā)現(xiàn)瀏覽器一直報錯Uncaught SyntaxError: Unexpected token {

所以按照官方文檔的格式,把js的結(jié)構(gòu)改成

<script> 
    new Vue({ 
      el:'.register-pannel',      
      data:{    
        show:true,   
        timer:null, 
        count:'' 
      },  
      methods:{   
        getCode(){ 
          this.show = false; 
          const TIME_COUNT = 60;    
          if (!this.timer) {     
            this.count = TIME_COUNT;     
            this.show = false;     
            this.timer = setInterval(() => {     
              if (this.count > 0 && this.count <= TIME_COUNT) {      
                this.count--;      
              } else {      
                this.show = true;      
                clearInterval(this.timer);      
                this.timer = null;      
              }     
            }, 1000)     
          }   
        }   
      } 
    }); 
    </script> 

于是格式是沒有問題了,但是樣式并沒有生效。變成了另一個樣子。

上網(wǎng)上搜了很多。

有說是js引用順序的問題。

有說是將js寫進(jìn)window.onload的。試了一下,發(fā)現(xiàn)都不對。

后來,在官方文檔中發(fā)現(xiàn)了el屬性:為實例提供掛載元素。值可以是 CSS 選擇符,或?qū)嶋H HTML 元素,或返回 HTML 元素的函數(shù)。

所以改成

<script>
 new Vue({
  el:'.register-pannel', //注冊div的class 
  data:{   
  show:true,  
  timer:null,
  count:''
  }, 
  methods:{  
  getCode(){
   this.show = false;
   const TIME_COUNT = 60;   
   if (!this.timer) {    
   this.count = TIME_COUNT;    
   this.show = false;    
   this.timer = setInterval(() => {    
    if (this.count > 0 && this.count <= TIME_COUNT) {     
    this.count--;     
    } else {     
    this.show = true;     
    clearInterval(this.timer);     
    this.timer = null;     
    }    
   }, 1000)    
   }  
  }  
  }
 });
</script>

效果就出來了。

總結(jié)

以上所述是小編給大家介紹vue實現(xiàn)驗證碼按鈕倒計時功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論

蒙城县| 确山县| 宁蒗| 厦门市| 柞水县| 色达县| 邯郸县| 莱西市| 门源| 西丰县| 澎湖县| 乃东县| 项城市| 西盟| 井冈山市| 岳阳市| 濮阳市| 蒙城县| 砀山县| 惠州市| 北川| 巴楚县| 文登市| 东平县| 南部县| 南平市| 宾阳县| 玉树县| 鹿邑县| 星子县| 城口县| 闽侯县| 宁南县| 沧源| 逊克县| 余庆县| 霍邱县| 阜南县| 南开区| 河池市| 中方县|