views:

317

answers:

3

I need to initiate the event handler after 3 seconds of being focused on an image. How should I go about it?

Also, I need to trigger another event handler, when I am at a particular part of an image, say the approximate middle of the image. How do I do this?

+1  A: 

For question #1: look into timers - you'd start it when the image is in focus (or the mouse hovers over it etc.), then it calls a function after 3 seconds (or any other period). The function would handle what you want to do after three seconds. (Maybe also check if the image is still "active".)

For question #2: One way to do this is Imagemaps, but there may be other/better options.

Hope this helps!

IronGoofy
thanks for answer to query #1.for query#2, image maps would be more html based. i need a solution for javascript...
amit
+2  A: 

Use javascript's setTimeout and setInterval functions.

// alert after 2 seconds
setTimeout("alert('Hello World!')", 2000);

// alert every 2 seconds
setInterval("alert('Hello, world!')", 2000);
aleemb
+4  A: 

JavaScript

var timeout;
function message(){
    alert('Hey there');
}

function start(){
    timeout = setTimeout(message,3000);
}

function stop(){
    clearTimeout(timeout);
}

HTML

<img src="HappyCow.jpg" onmouseover="start()" onmouseout="stop()" />

The event handling is rough here (inline >.<), but I think this gets you started.

rpflo
Thanks Gumbo, I just re-wrote the code exactly as you did and hit refresh to edit my answer and thought ... "magic?", then noticed you edited it.
rpflo