setTimeout start without reason and can't be stopped

Exia0890

I activate a tooltip when I move the mouse over an image :

<img src="images/F9903.jpg" alt="fruit1" width="200" height="200" onmouseover="imgover(this, document.getElementById('text1').innerHTML)" onmouseout="imgout()" /> 
............
 <div id="tooltip6" onmouseover="imgover_tooltip6()" >Tooltip Text</div>             

And it will disappear after I move away the cursor. Here are the javascript code relative to it:

    function imgover(img, tip) {


        document.getElementById('tooltip6').innerHTML = tip;
        var DivHeight = document.getElementById('tooltip6').scrollHeight;
        //alert(DivHeight);

        //document.getElementById('tooltip6').style.display = 'block';
        document.getElementById('tooltip6').style.height = DivHeight + 'px';
        document.getElementById('tooltip6').style.opacity = '1';
        document.getElementById('tooltip6').style.overflow = 'visible';



    }
    var timeVar;

    function imgout() {

       timeVar =  setTimeout(function () {
            document.getElementById('tooltip6').style.height = '0px';
            document.getElementById('tooltip6').style.opacity = '0';
            document.getElementById('tooltip6').style.overflow = 'hidden';

        }, 3000);
    }   

    function imgover_tooltip6() {
        clearTimeout(timeVar);
    }

There are many issues here:

Even when I put in comment the function imgover_tooltip6(), the setTimeout will work fine only the first time, and then when I move again my cursor over the image, the tooltip will disappear (sometimes right away) even though the cursor is still on the image.

And when I move the cursor over the tooltip (before it disappear) the setTimeOut is not stopped, and the tooltip will still disappear.

Here is a link to my site to illustrate the exemple ( the images concerned are those of the carousel in the top)

Greenlandi

If I understood your problem correctly, you must call clearTimeout(timeVar) before setting setTimeout again to the same variable.

function imgover(img, tip) {
   clearTimeout(timeVar);
   .....
}
function imgout() {
   clearTimeout(timeVar);

   timeVar =  setTimeout(function () {
        document.getElementById('tooltip6').style.height = '0px';
        document.getElementById('tooltip6').style.opacity = '0';
        document.getElementById('tooltip6').style.overflow = 'hidden';

    }, 3000);
}

The reason why the tooltip gets hidden after hovering over the image multiple times, is that when you hover over the image for the first time, timeVar gets the id of the first timer, 1, and when hovering over for the second time, it gets 2. Now when the clearTimeout(timeVar) is called, it clears timer 2, but 1 is still left running and will not get cleared, because timeVar didn't contain it's id anymore.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related