如何设置时间限制

时间:2011-12-20 23:58:10

标签: javascript

我正在尝试为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 个答案:

答案 0 :(得分:1)

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);
    }
}