JavaScript 高级课程之定时器setInterval,clearInterval

来源:互联网 发布:做淘宝一个月赚10万 编辑:程序博客网 时间:2024/05/17 22:11
<!doctype html><html><head><meta charset="utf-8"><title>JavaScript 高级课程之定时器setInterval,clearInterval</title><style>#div1 { width:100px; height:100px; background-color:#005812; position:absolute}</style><script>window.onload = function(){    var oDiv = document.getElementById('div1');    var oInp = document.getElementById('inp1');    var timer = null;    oInp.onclick = ac;        function ac(){        clearInterval(timer); //先关后开,防止重复定时器        timer = setInterval(function (){            if(oDiv.offsetLeft >= 300){                clearInterval(timer);                }else{                    oDiv.style.left = oDiv.offsetLeft + 1 +'px';                 }            },30)        }}</script></head><body><input type="button" value="移动" id="inp1"/><div id="div1"></div></body></html>

0 0
原创粉丝点击