如何设置时间限制

如何设置时间限制

问题描述:

我想时间限制添加到Javascript代码,如何设置时间限制

你能不能帮我修改,

详情: 有一个按钮,鼠标将遵循这个按钮,我想要做的是鼠标将不会在x秒后的按钮。

下面是代码:

<script> 
var iflag = 0; 
var icontainer = document.getElementById('icontainer');  
var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body 



function mouseFollower(e){ 
if (window.event) 
{ // for IE 
    icontainer.style.top = (window.event.y-5)+standardbody.scrollTop+'px'; 
    icontainer.style.left = (window.event.x-5)+standardbody.scrollLeft+'px'; 
} 
else 
{ 
    icontainer.style.top = (e.pageY-5)+'px'; 
    icontainer.style.left = (e.pageX-5)+'px'; 
} 

} 
document.onmousemove = function(e) { 
    if (iflag == 0) {mouseFollower(e);} 
} 

</script> 
+1

你必须要更加具体。很难说出你在问什么。 – FishBasketGordo 2011-12-20 23:59:50

+1

你对JavaScript代码的时间限制有什么了解? – 2011-12-21 00:01:08

+0

有一个按钮,它跟随着鼠标。我想设置时间限制,因此按钮将在x秒后消失。 – 2011-12-21 00:01:38

var startTime = null; //we haven't started yet 
var limit = 10000; //10 seconds 

document.onmousemove = function(e) { 
    var now = new Date(); 

    // set startTime to now if this is the first run i.e. it doesn't have a value 
    // so we can tell when we started 
    var startTime = startTime || now; 

    // if we've been running longer than limit 
    if (now >= startTime + limit) { //using a Date as a scalar gets a timestamp 
     // delete this function so it can't run again 
     delete document.onmousemove; 
    } else { 
     // do following stuff 
     mouseFollower(e); 
    } 
} 
+0

我添加了第一行: var iflag = 0; var icontainer = document.getElementById('icontainer'); var standardbody =(document.compatMode ==“CSS1Compat”)? document.documentElement中:document.body的 然后添加其他线路后: document.onmousemove =功能(E){ 但它不会跟随鼠标和10秒后 什么是我的错按钮不会出现? 谢谢 – 2011-12-21 00:41:57