Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript event handler to initiate after 3 seconds

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?

like image 618
amit Avatar asked Apr 23 '26 22:04

amit


2 Answers

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);
like image 193
aleemb Avatar answered Apr 25 '26 13:04

aleemb


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.

like image 45
Ryan Florence Avatar answered Apr 25 '26 12:04

Ryan Florence



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!