Issue w/ setTimeout

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • korinth
    New Member
    • Apr 2007
    • 5

    Issue w/ setTimeout

    I'm trying to change the visibility of divs on a mouseover event. I have the script working that will do this, but when I try to use it with a setTimeout method, I cannot get it to work. My code is as follows:


    function slide(name) {
    setTimeout("dis play(name)", 1000);
    }

    function display(objname ){

    var caseNum;

    if (objname == document.getEle mentById("illHe ader")) {caseNum = 0;}
    if (objname == document.getEle mentById("webHe ader")) {caseNum = 1;}
    if (objname == document.getEle mentById("mainH eader")) {caseNum = 2;}
    if (objname == document.getEle mentById("flash Header")) {caseNum = 3;}


    switch(caseNum) {
    case 0: document.getEle mentById("ill") .style.display = "block";
    document.getEle mentById("web") .style.display = "none";
    document.getEle mentById("main" ).style.display = "none";
    document.getEle mentById("flash ").style.displa y = "none";
    break;
    case 1: document.getEle mentById("web") .style.display = "block";
    document.getEle mentById("ill") .style.display = "none";
    document.getEle mentById("main" ).style.display = "none";
    document.getEle mentById("flash ").style.displa y = "none";
    break;
    case 2: document.getEle mentById("main" ).style.display = "block";
    document.getEle mentById("ill") .style.display = "none";
    document.getEle mentById("web") .style.display = "none";
    document.getEle mentById("flash ").style.displa y = "none";
    break;
    case 3: document.getEle mentById("main" ).style.display = "none";
    document.getEle mentById("ill") .style.display = "none";
    document.getEle mentById("web") .style.display = "none";
    document.getEle mentById("flash ").style.displa y = "block";
    break;
    }
    }

    The html is as follows <div id="whatever" onmouseover="sl ide(this);">

    I know there's probably a simple fix to this that's going to make me feel stupid, but at this point I'm willing to feel stupid as long as it works. Thanks!
  • Logician
    New Member
    • Feb 2007
    • 210

    #2
    Originally posted by korinth
    I'm trying to change the visibility of divs on a mouseover event. I have the script working that will do this, but when I try to use it with a setTimeout method, I cannot get it to work. My code is as follows:
    Code:
    function slide(name) {
    	setTimeout("display(name)", 1000);
    }
    name will be out of scope when display is called, which must be generating an error.
    I would pass the id rather than a reference to the element:
    Code:
    <div id="main" onmouseover="slide(this.id);">content</div>
    then
    Code:
    function slide(divId)
    {
     setTimeout("display('"+divId+"')", 1000);
    }
    Then display can be simplified to
    Code:
    function display(objId)
    {
     var objRef=document.getElementById(objId);
       
     var divIds=["ill","web","main","flash"];
    
     for( var i=0, divRef; i<divIds.length; i++ )
      (divRef=document.getElementById( divIds[i] ) ).style.display = (divRef==objRef ? 'block' : 'none' );
    }
    I don't recommend the mouseover event for this operation.

    Comment

    Working...