display image and script for a set period of time

Posted by Ryan Max on Stack Overflow See other posts from Stack Overflow or by Ryan Max
Published on 2010-05-03T00:38:00Z Indexed on 2010/05/03 0:47 UTC
Read the original article Hit count: 644

Filed under:
|
|
|

This is very similar to a question I asked the other day but my page code has become significantly more complicated and I need to revisit it. I've been using the following code:

$('#myLink').click(function() {
  $('#myImg').attr('src', 'newImg.jpg');
  setTimeout(function() { $('#myImg').attr('src', 'oldImg.jpg'); }, 15000);
});

To replace an image for a set period of time (15 seconds) when the link is clicked, then after 15 seconds, revert to the original image.

However now, I'd like to run a snippet of javascript as well when the link is clicked (in addition to replacing the image), and only when the link is clicked (it's related to the 15 second image) and then have the js code disappear as well after the 15 seconds...however I'm not sure how to have jquery send js code into the page...Basically I just want jQuery to "echo" this code onto the page underneath the 15 second while I am there, but I don't know how jquery formats this "echo".

Does this question make sense?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript