views:

40

answers:

3

I want an image, which is not clicked after 5 seconds, to turn into another image, which then returns to the original image when clicked.

So

image > (user not clicked on image after 5 secs) > image prompting user to click > (user clicks) > image back to original.

Any ideas?

+1  A: 

This is very easy with jquery

what we will do is: when the document is ready, get all the images with class "promptimage", set a timer on them, when timer passes -> change their source to another source, and when it is clicked, set it to default src.

$(function(){
   var original_src = $('.promptimage').attr("src");
   $('.promptimage').attr("src",'set your image source with the prompting image').delay(800);
   $('.promptimage').click(function(){
     $('.promptimage').attr("src",original_src);
   });
});
Nealv
That is not an answer
ILMV
You can always delete your answer until you've actually finished it... ;-) From what you've said so far though you aren't taking into account that the image doesn't want to change on document ready but five seconds later (as I read it anyway).
Chris
I was editing my post
Nealv
With this code, the new image appears really quickly (and in fact changing the time delay of 800 to 99999999 it still appears just as quickly) and when you click the image it does not revert back to the original. The image in question when clicked expands to reveal the content (I am using another script), will this affect things? Thanks for your reply.
Sean McRaghty
maybe if u share youre code, we can check it
Nealv
Well the code you have given me, customized, is: http://pastebin.com/Y8EcdKNn , and Firebug is telling me: $(".promptimage").attr("src", "sites/all/themes/garhoo/kahoosclick.png").delay is not a function
Sean McRaghty
It looks like the image that is supposed to load after a certain time is actually loading when the document is ready, and that would explain why changing the timer doesn't affect when it appears.
Sean McRaghty
A: 

I'm not sure exactly where you are having problems so I'll give a general overview of what I think is needed:

Image should have an onclick event that can determine when it has been clicked. This can set a flag and reset the image if necessary. It can also cancel the timer in the next step if it is still running.

On page load set a 5 second timer. Once that is up run a function that checks if the image has been clicked. If it hasn't change the image. If it has do nothing.

I'd suggest search or reasking a new question on specific bits you are having problems with.

Chris
+1  A: 

Sure, not a problem. You can do it with the following code:

HTML

<img id="clickable" src="http://fullahead.org/gallery/toronto-2010/image/thumb/DSC_3356.jpg" style="cursor: pointer" title="Clicky!"/>

Javascript

// Get a reference to the image and save its original source
var img = document.getElementById('clickable');
var origSrc = img.src;

// Create the timeout to change the source (2 seconds in code below)
var timeout = setTimeout(function(){
    img.src = 'http://fullahead.org/gallery/ilse-de-grand-calumet-2010/image/thumb/DSC_3163.jpg';   
}, 2000);

// Register the click event handler for the image and clear the timeout
img.onclick = function(){
    clearTimeout(timeout);
    this.src = origSrc;    
}

You can see it in action here.

Pat
Works only once, though.
Litso
@Litso - as per the specs ;)
Pat
You'd only need to start the timer again to make it repeatable.
Brent Friar
Just looking at the jsFiddle, would I need to load 'vanillaJS' into my page?
Sean McRaghty
@pat, you're right, I read too quickly.
Litso
@Sean McRaghty - nope, `vanillaJS` is used to tell jsFiddle that the code is standard Javascript with no special libraries.
Pat