#1
  1. Purple is a Fruit
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Ireland
    Posts
    404
    Rep Power
    170

    Executing a JavaScript function after a short period of time


    Hi,

    I want a div to disappear 1 second after I mouse out on it. I have a function to hide the div:

    function pullUp(menu){
    if(whatSelect!='')
    document.getElementById(menu).style.visibility = 'hidden';
    }

    I have tried adding wait(1000); after if (whatSelect!=' ') but I get the error: Object expected. Can wait be used like: wait (1000, pullUp(menu)); passing the function to be run after the set amount of time?

    Thanks
  2. #2
  3. Corporate Stooge
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Aberdeen, Scotland
    Posts
    134
    Rep Power
    14
    Use setTimeout:

    setTimeout("yourfunction()",1000);
    Never sign your code....it leaves you liable!
  4. #3
  5. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    First: rtfm.
    No sense just typing stuff in.

    Second: a bit more to it than just setting a timer:
    you need to be able to cancel it if the user mouses back over again.

    var hiderID = null;

    function getElement(id) {
    return document.getElementById ? document.getElementById(id) :
    document.all ? document.all(id) : null;
    }

    function hide() {
    for (var el,a=0; a<arguments.length; ++a) {
    el = getElement(arguments[a]);
    if (el && el.style) el.style.visibility = 'hidden';
    }
    }

    function show() {
    for (var el,arg,a=0; a<arguments.length; ++a) {
    el = getElement(arguments[a]);
    if (el && el.style) el.style.visibility = 'visible';
    }
    }

    function whatever(menu) { // mouseover
    if (hiderID) clearTimeout(hiderID);
    ....other statements
    }

    function pullUp(menu) { // mouseout
    if(whatSelect!='')
    hiderID = setTimeout('hide("'+menu+'")',1000);
    }

    Both hide() & show() take multiple arguments.

IMN logo majestic logo threadwatch logo seochat tools logo