代码是学习别人的,我的目的就是学习别人的代码,组建自己的代码库。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>计时器</title> </head> <body> <input id="timeBox" type="text" name="name" value="" /> <input id="start" type="button" name="name" value="开始" /> <input id="stop" type="button" name="name" value="停止" /> <input id="clear" type="button" name="name" value="清空" /> <script type="text/javascript"> //声明一个全局的number,保存定时器的ID var number; //在 timebox 框中显示当前时间 //1、获取当前时间 function fill(){ var now = new Date(); var nowStr = now.toLocaleString();//2018/4/8 上午9:28:00 var nowDate = now.toLocaleDateString();//2018/4/8 var nowTime = now.toLocaleTimeString(); //上午9:27:23 //2、把时间字符串填充到 input 框里 var tbox = document.getElementById("timeBox"); tbox.value = nowStr; } //点击开始时,动起来 //找到开始按钮,给他绑定事件 var startButton = document.getElementById("start"); startButton.onclick=function() { fill(); if (number === undefined) { //把定时器的 id 复制给之前声明的全局变量 t number = setInterval(fill, 1000); } } var stopButton = document.getElementById("stop"); stopButton.onclick = function () { //清除定时器 clearInterval(number); number = undefined; } var clearButton = document.getElementById("clear"); clearButton.onclick = function () { clearInterval(number); number = undefined; //3、清空 input 框里 var tbox = document.getElementById("timeBox"); tbox.value = ""; } </script> </body> </html>
获取参数中的值
/** * 获取浏览器参数 * @param {any} url */ function getUrlParam(url) { var queryArr = (url && url.substr(url.indexOf('?') + 1).split('&')) || location.search.substr(1).split('&'), query = {}; for (var i = 0, len = queryArr.length; i < len; i++) { var key = queryArr[i].split('=')[0], val = queryArr[i].split('=')[1]; if (query.hasOwnProperty(key)) { if (!Array.isArray(query[key])) { query[key] = [query[key]] query[key].push(val) } else { query[key].push(val) } continue } query[key] = val } return query }