1 var random=""; 2 function getMessages(button){ 3 //前臺倒計時 4 front(button); 5 //請求後臺發送驗證碼 6 behind(); 7 } 8 9 ///後臺發送短信 10 function behind (){ 11 //隨機碼 12 random=getRandom(); 13 var tel=document.getElementById("tel").value; 14 $.post("<%=path%>/sendSmsServlet.action",{tel:tel,random:random},function(data){ 15 16 }); 17 } 18 19 //前臺實現倒計時 20 function front(button){ 21 //前臺倒計時 22 var count = 60; 23 button.value = count + "s後可從新獲取驗證碼"; 24 //將按鈕設置爲不可點擊 25 button.setAttribute("disabled", "disabled"); 26 var time = setInterval(function() { 27 count--; 28 button.value = count + "s後可從新獲取驗證碼"; 29 if (count <= 0) { 30 //關閉定時器 31 clearInterval(time); 32 //改變按鈕標題 33 button.value = "獲取驗證碼"; 34 button.removeAttribute("disabled"); 35 } 36 }, 1000); 37 } 38 39 //獲取四位隨機完整碼 40 function getRandom(){ 41 var str=""; 42 for(var i=0;i<4;i++){ 43 var num=parseInt(Math.random()*1000000000%10); 44 str+=num; 45 } 46 return str; 47 } 48