JS九 发表于 2015-09-14 | 分类于 javascript 定时器 点击倒计时html1234<div class="box"> <input type="text"/> <button>点击发送短信</button></div> script12345678910111213141516171819202122232425262728window.onload = function(){ var btn = document.getElementsByTagName("button")[0]; var count = 5; //多少秒倒计时 var timer = null; // 定时器的名字 btn.onclick = function(){ clearInterval(timer); // 先清除掉原来的定时器 this.disabled = true; // this 指向的是 btn var that = this; // 把 btn 对象 给 that timer = setInterval(send,1000); function send(){ // alert(this); // this 指向的是 定时器 window // alert(that); count--; if(count >= 0){ that.innerHTML = '还有'+ count + "秒"; } else { that.innerHTML = "重新发送短信"; that.disabled = false; clearInterval(timer); // 清除定时器 count = 5; } } }} 页面多少秒后跳转html1<div id="demo"></div> script12345678910var demo = document.getElementById("demo");var count = 5;setInterval(fn,1000);function fn(){ count--; demo.innerHTML = "页面将在" + count + "后跳转"; if(count <= 0){ window.location.href = "http://www.baidu.com"; }} 转换为字符串1234var txt = 12345;console.log(typeof (txt + "")); //stringconsole.log(typeof String(txt)); //stringconsole.log(typeof txt.toString()); //string 字符串常见操作方法12345678910111213141516var txt1 = "abcdefg"var txt2 = "123456";var txt3 = "一二三四五六"var txt4 = " 123456 "//返回指定索引位置的字符console.log(txt1.charAt(3)); //dconsole.log(txt2.charAt(3)); //4console.log(txt3.charAt(3)); //四//提取字符串的片断,并在新的字符串中返回被提取的部分console.log(txt1.slice(1,4)); //bcdconsole.log(txt2.slice(1,4)); //234console.log(txt3.slice(1,4)); //二三四//移除字符串首尾空白console.log(txt4.trim());