Results 1 to 4 of 4

Thread: Run a function after 2 sec

  1. #1

    Thread Starter
    New Member webx2000's Avatar
    Join Date
    May 2003
    Posts
    9

    Run a function after 2 sec

    Hi there, I'm designing a web page in which I want to change a cell color if the mouse has been within the cell for 2 seconds. Can somebody help me in this one, thanks
    Last edited by webx2000; Oct 11th, 2003 at 12:29 AM.

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    I didn't add browser compatability, so it's pretty much IE specific:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <TITLE></TITLE>
    	<script language="javascript" type="text/javascript">
    		var timeoutId = 0;
    		var currentCell = "";
    		
    		var hoverColor = "silver";
    		var normalColor = "";
    		var timeoutLength = 2000;
    		
    		function setCellColor(cellId, color) {
    			var obj = document.all[cellId];
    			if (obj != null) {
    				obj.style.backgroundColor = color;
    			}
    			removeTimeout();
    		}
    		
    		function cellMouseOver(cellId) {
    			if (currentCell != "") {
    				setCellColor(currentCell, normalColor);
    			}
    			removeTimeout();
    			
    			currentCell = cellId;
    			timeoutId = setTimeout("setCellColor('" + cellId + "', '" + hoverColor + "');", timeoutLength);
    		}
    		
    		function cellMouseExit(cellId) {
    			removeTimeout();
    			setCellColor(cellId, normalColor);
    		}
    		
    		function removeTimeout() {
    			if (timeoutId != 0) {
    				clearTimeout(timeoutId);
    				timeoutId = 0;
    			}
    		}
    	</script>
    </HEAD>
    <BODY>
    
    <table border="0" cellpadding="2" cellspacing="0">
    	<tr>
    		<td id="tdCell1" onmouseenter="cellMouseOver('tdCell1');" onmouseleave="cellMouseExit('tdCell1');">Cell 1</td>
    		<td id="tdCell2" onmouseenter="cellMouseOver('tdCell2');" onmouseleave="cellMouseExit('tdCell2');">Cell 2</td>
    	</tr>
    </table>
    
    </BODY>
    </HTML>

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Replace onmouseenter with onmouseover, onmouseleave with onmouseout and document.all[id] with document.getElementById(id) and it should work on all important browsers: IE5+, Mozilla, NS6+, Opera 6+, Konqueror and Safari.

    Even easier, pass this instead of the cell ids in the event handler and forget about the document.all/document.gEBI completly.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    New Member webx2000's Avatar
    Join Date
    May 2003
    Posts
    9
    Thank u all for ur help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width