定时器函数的应用setInterval()

setInterval(function(){},time)
该函数表示每个time的时间就执行function(){}
下列列子是在js里面实现一个div自动运动
var div=document.createElement(‘div’); //创建一个div
document.body.appendChild(div); //往body添加div
div.style.width=“100px”;
div.style.height=“100px”;
div.style.backgroundColor=“red”;
div.style.position=“absolute”;
div.style.top=“20px”;
div.style.left=“20px”;*/
var timer=setInterval (function() {
div.style.left=parseInt(div.style.left)+10+“px”;
div.style.top=parseInt(div.style.top)+10+“px”;
console.log(div.style.left);
if(parseInt(div.style.left)>300){
clearInterval(timer); //停止定时器
}
},100)

猜你喜欢

转载自blog.csdn.net/FateInTheSky/article/details/82775600