setTimeout只在指定时间后执行一次
- <script>
- //定时器 异步运行
- function hello(){
- alert("hello");
- }
- //使用方法名字执行方法
- var t1 = window.setTimeout(hello,1000);
- var t2 = window.setTimeout("hello()",3000);//使用字符串执行方法
- window.clearTimeout(t1);//去掉定时器
- </script>
setInterval以指定时间为周期循环执行
- //实时刷新时间单位为毫秒
- setInterval('refreshQuery()',8000);
- /* 刷新查询 */
- function refreshQuery(){
- $("#mainTable").datagrid('reload',null);
- }
//放入移开 鼠标事件,js 左右运动。
<!DOCTYPE html>
<html><head>
<meta charset="utf-8" />
<title></title>
</head>
<style>
div,body{
border: 0;margin: 0;padding: 0;
}
#btn{
position: relative;
top: 0;
left: -300px;
width: 300px;
height: 300px;
background-color: aqua;
}
#btn span{
font-size: 18px;
width: 20px;
height: 50px;
background-color:darksalmon ;
position: absolute;
left: 300px;
top: 140px;
}
</style>
<script>
window.onload = function(){
var odiv = document.getElementById("btn");odiv.onmouseover = function(){
startMove(20,0);
}
odiv.onmouseout = function(){
startMove(-20,-300);
}
}
var timer = null;
function startMove(sudu,zhongdianzhi){
clearInterval(timer);
var odiv = document.getElementById("btn");
timer = setInterval(function(){
if (odiv.offsetLeft == zhongdianzhi) {
clearInterval(timer);
} else{
odiv.style.left = odiv.offsetLeft + sudu + 'px';
}
},30)
}
</script>
<body>
<div id="btn"><span id="share">分享</span></div>
</body>
</html>