事件队列,求助~! 本帖最后由 h123hu 于 2012-02-21 11:41:31 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你发过一个模块移动的贴子对不对,我改写代码实现后,找不到主题了。出现问题的原因是,onmousemove会多次触发,声明的变量Time在还没有清除,就赋值给了新setInterval(...),所以旧的计时函数得以持续进行。改写方法是Time=[]模拟一个FIFO队队,使用push(setInerval(...)),if判断里边使用clearInterval(Time.shift()),就可以解决问题。 注意你的if判断在修改style.left之后,所以div2总会移动。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> #div1 { width: 948px; height: 40px; overflow: hidden; position: relative; border: 1px #efefef solid; } #div2 { width: 40px; height: 40px; overflow: hidden; position: absolute; background-color: #bf0000; top: 0px; left: 0px; } </style> <script type="text/javascript"> //获取鼠标坐标 function getMouseXY(ev) { if (ev.pageX || ev.pageY) { return { x: ev.pageX, y: ev.pageY }; } return { x: ev.clientX + document.body.scrollLeft - document.body.clientLeft, y: ev.clientY + document.body.scrollTop - document.body.clientTop }; } //获取元素左坐标 function getOffsetLeft(obj) { var left = 0; var offsetParent = obj; while (offsetParent != null && offsetParent != document.body) { left += offsetParent.offsetLeft; offsetParent = offsetParent.offsetParent; } return left; } window.onload = function () { var div1 = document.getElementById('div1'); //获取父级ID var div2 = document.getElementById('div2'); //获取子级ID var time = []; //计时器 var output = function (id, text) { document.getElementById(id).innerHTML += text + '<br/>'; }; div1.onmousemove = function () { time.push(setInterval(function () { var left1 = getOffsetLeft(div2); div2.style.left = left1 + 1 + 'px'; var left2 = getOffsetLeft(div2); output('p1', 'time.push() : ' + time + ', left: ' + left2); if (left2 >= 100) { clearInterval(time.shift()); output('p1', 'time.shift(): ' + time + ', left: ' + left2); } }, 1)); }; } </script> </head> <body> <div id="div1"> <div id="div2"></div> </div> <hr /> <p id="p1"></p> </body></html> 一段出入栈记录示例如下:time.push() : 1, left: 19time.push() : 1, left: 29time.push() : 1, left: 39time.push() : 1, left: 49time.push() : 1,2, left: 59time.push() : 1,2, left: 69time.push() : 1,2, left: 79time.push() : 1,2, left: 89time.push() : 1,2,3, left: 99time.push() : 1,2,3, left: 109time.shift(): 2,3, left: 109time.push() : 2,3, left: 119time.shift(): 3, left: 119time.push() : 3,4, left: 129time.shift(): 4, left: 129time.push() : 4, left: 139time.shift(): , left: 139time.push() : 5, left: 149time.shift(): , left: 149time.push() : 6, left: 159time.shift(): , left: 159可以看出,进栈的setInterval()都被弹出了,所以小块能停止移动。 JS动态生成复选框 求助!!! 郁闷:javascript动态显示textarea中输入的字符长度,超过制定长度要拦截住 获取div是style属性值 javascript 随便提交什么验证都通过啊!这个页面的Javascript正则表达式要怎么写呢?高手帮忙啊!谢谢啦!!!! 被恶意网站www.58q.com篡改的IE配置,如何恢复? 如何将字符串转换为变量名 div style设置没有用,求大神解释 长连接问题 求救 JS驱动下拉列表出问题 if的两个条件怎么写? 正则表达式,高人进。
改写方法是Time=[]模拟一个FIFO队队,使用push(setInerval(...)),if判断里边使用clearInterval(Time.shift()),就可以解决问题。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
#div1 {
width: 948px;
height: 40px;
overflow: hidden;
position: relative;
border: 1px #efefef solid;
}
#div2 {
width: 40px;
height: 40px;
overflow: hidden;
position: absolute;
background-color: #bf0000;
top: 0px;
left: 0px;
}
</style>
<script type="text/javascript">
//获取鼠标坐标
function getMouseXY(ev) {
if (ev.pageX || ev.pageY) {
return {
x: ev.pageX,
y: ev.pageY
};
}
return {
x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y: ev.clientY + document.body.scrollTop - document.body.clientTop
};
} //获取元素左坐标
function getOffsetLeft(obj) {
var left = 0;
var offsetParent = obj;
while (offsetParent != null && offsetParent != document.body) {
left += offsetParent.offsetLeft;
offsetParent = offsetParent.offsetParent;
}
return left;
} window.onload = function () {
var div1 = document.getElementById('div1'); //获取父级ID
var div2 = document.getElementById('div2'); //获取子级ID
var time = []; //计时器
var output = function (id, text) {
document.getElementById(id).innerHTML += text + '<br/>';
}; div1.onmousemove = function () {
time.push(setInterval(function () {
var left1 = getOffsetLeft(div2);
div2.style.left = left1 + 1 + 'px';
var left2 = getOffsetLeft(div2);
output('p1', 'time.push() : ' + time + ', left: ' + left2);
if (left2 >= 100) {
clearInterval(time.shift());
output('p1', 'time.shift(): ' + time + ', left: ' + left2);
}
}, 1));
}; }
</script>
</head>
<body>
<div id="div1">
<div id="div2"></div>
</div>
<hr />
<p id="p1"></p>
</body></html>
time.push() : 1, left: 19
time.push() : 1, left: 29
time.push() : 1, left: 39
time.push() : 1, left: 49
time.push() : 1,2, left: 59
time.push() : 1,2, left: 69
time.push() : 1,2, left: 79
time.push() : 1,2, left: 89
time.push() : 1,2,3, left: 99
time.push() : 1,2,3, left: 109
time.shift(): 2,3, left: 109
time.push() : 2,3, left: 119
time.shift(): 3, left: 119
time.push() : 3,4, left: 129
time.shift(): 4, left: 129
time.push() : 4, left: 139
time.shift(): , left: 139
time.push() : 5, left: 149
time.shift(): , left: 149
time.push() : 6, left: 159
time.shift(): , left: 159
可以看出,进栈的setInterval()都被弹出了,所以小块能停止移动。