最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - stop function that run with setTimeout - Stack Overflow

programmeradmin4浏览0评论

I want stop my function that run with setTimeout and do not show image followed mouse. I want do that with button click, how do that? my code:

<html xmlns="">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
    var trailimage = ["test.gif", 100, 99];
    var offsetfrommouse = [-25, -25];
    var displayduration = 0;
    function truebody() {
        return (!window.opera && documentpatMode && documentpatMode != "BackCompat") ? document.documentElement : document.body;
    }
    function hidetrail() {
        var x = document.getElementById("trailimageid").style;
        x.visibility = "hidden";
        document.onmousemove = "";
    }
    function followmouse(e) {
        var xcoord = offsetfrommouse[0];
        var ycoord = offsetfrommouse[1];
        if (typeof e != "undefined") {
            xcoord += e.pageX;
            ycoord += e.pageY;
        }
        else if (typeof window.event != "undefined") {
            xcoord += truebody().scrollLeft + event.clientX;
            ycoord += truebody().scrollTop + event.clientY;
        }
        var x = document.getElementById("trailimageid").style;
        x.left = xcoord + "px";
        x.top = ycoord + "px";            
    }

        alert("obj_selected = true");
        document.onmousemove = followmouse;
        if (displayduration > 0)
            setTimeout("hidetrail()", displayduration * 1000);

</script>
</head>
<body>
<form id="form1" runat="server">

    <img alt="" id="trailimageid" src="Pictures/sides/sides-not-clicked.gif" border="0" style="position: absolute; visibility: visible; left: 0px;
    top: 0px; width: 50px; height: 50px"/>

</form>
 </body>
</html>

I want stop my function that run with setTimeout and do not show image followed mouse. I want do that with button click, how do that? my code:

<html xmlns="http://www.w3/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
    var trailimage = ["test.gif", 100, 99];
    var offsetfrommouse = [-25, -25];
    var displayduration = 0;
    function truebody() {
        return (!window.opera && document.patMode && document.patMode != "BackCompat") ? document.documentElement : document.body;
    }
    function hidetrail() {
        var x = document.getElementById("trailimageid").style;
        x.visibility = "hidden";
        document.onmousemove = "";
    }
    function followmouse(e) {
        var xcoord = offsetfrommouse[0];
        var ycoord = offsetfrommouse[1];
        if (typeof e != "undefined") {
            xcoord += e.pageX;
            ycoord += e.pageY;
        }
        else if (typeof window.event != "undefined") {
            xcoord += truebody().scrollLeft + event.clientX;
            ycoord += truebody().scrollTop + event.clientY;
        }
        var x = document.getElementById("trailimageid").style;
        x.left = xcoord + "px";
        x.top = ycoord + "px";            
    }

        alert("obj_selected = true");
        document.onmousemove = followmouse;
        if (displayduration > 0)
            setTimeout("hidetrail()", displayduration * 1000);

</script>
</head>
<body>
<form id="form1" runat="server">

    <img alt="" id="trailimageid" src="Pictures/sides/sides-not-clicked.gif" border="0" style="position: absolute; visibility: visible; left: 0px;
    top: 0px; width: 50px; height: 50px"/>

</form>
 </body>
</html>
Share Improve this question asked Apr 26, 2011 at 6:12 hamze torabzadehamze torabzade 7,3416 gold badges36 silver badges43 bronze badges 1
  • In the posted code you do not execute the timeout since the displayduration is 0. Also I suggest you change setTimeout("hidetrail()", displayduration * 1000); to someVar = setTimeout(hidetrail, displayduration * 1000); where someVar is defined where your clearTimeout can get to it. Removing the quotes removes the hidden eval which is considered evil – mplungjan Commented Apr 26, 2011 at 6:21
Add a ment  | 

3 Answers 3

Reset to default 6
var foobarTimeout = setTimeout(foobar, 1000);

...

clearTimeout(foobarTimeout);

See:
https://developer.mozilla/en/DOM/window.setTimeout
https://developer.mozilla/en/DOM/window.clearTimeout

Save the return value of setTimeout, which is a "handle" for the timer, and when you want to cancel it, call clearTimeout with that value.

So in your code, you'd declare a timerHandle variable somewhere appropriate, then set it here:

if (displayduration > 0)
    timerHandle = setTimeout("hidetrail()", displayduration * 1000);

...and then create a button click handler:

function cancelTimeoutOnClick() {
    if (timerHandle) {
        clearTimeout(timerHandle);
        timerHandle = 0;
    }
}

Off-topic: It's almost never best practice to pass strings into setTimeout, that's an implicit eval. In your case, just pass the function reference:

if (displayduration > 0)
    timerHandle = setTimeout(hidetrail, displayduration * 1000);
                          // ^--- Difference here (no quotes, no parentheses)

you use timeout like >

var myTimeout = setTimeout(yourfunction);

and then you can cancel it >

clearTimeout(myTimeout);
发布评论

评论列表(0)

  1. 暂无评论