views:

163

answers:

3

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?

  interval = 500;
  imgsrc = "webcam/image.jpg";

  function Refresh() {
     tmp = new Date(); 
     tmp = "?" + tmp.getTime();
     document.images["image1"].src = imgsrc + tmp;
     setTimeout("Refresh()", interval);
  }

  Refresh();

It's script for a webcam. Basically it takes a new picture every half a second and replaces it on the page. You must forgive me, I'm new to jquery, I don't know how to make this script work in a jquery context.

A: 

It probably depends on what the code does.

Don't think in terms of the page source. Think in terms of the DOM.

It's likely that the javascript you want to inject handles some kind of event (it's an event handler). So on the initial click, you want to bind (attach) that function to the proper event, and then unbind it in the setTimeout'd function.

If you're not trying to enable/disable some behavior, then we could use more information about what your this temporary javascript is doing to provide better guidance.

timdev
i've posted the code that I am using in an answer.
Ryan Max
+2  A: 

If you want to run something at an interval, use setInterval(), which is similar to setTimeout().

If you assign setInterval() to a variable, you can use that variable to "clear" it. Do that in your setTimeout() function.

$('#myLink').click(function() {
    // Start setInterval which runs a function every n seconds, and assign it to a variable
    var runningInterval = setInterval(function() {...}, 500);  

    $('#myImg').attr('src', 'newImg.jpg');

    // After 15 seconds, clear the runningInterval, and reset the image.
    setTimeout(function() { clearInterval(runningInterval); $('#myImg').attr('src', 'oldImg.jpg'); }, 15000);
});

EDIT:

Ok, so, here it is with your code.

  interval = 500;
  imgsrc = "webcam/image.jpg";

  // Is this function getting called?
  function Refresh() {
     tmp = new Date(); 
     tmp = "?" + tmp.getTime();

        // You could use jQuery here, just like below.
     document.images["image1"].src = imgsrc + tmp;
  }

  $('#myLink').click(function() {
      // Start setInterval which runs your Refresh function every 0.5 seconds, and assign it to a variable
      var runningInterval = setInterval(Refresh, interval);  

      // Display new image
      // This line may be unnecessary if the Refresh() function is loading it anyway.
      $('#myImg').attr('src', 'newImg.jpg');

      // After 15 seconds, clear the runningInterval, and reset the image.
      setTimeout(function() { clearInterval(runningInterval); $('#myImg').attr('src', 'oldImg.jpg'); }, 15000);

      return false;
  });
patrick dw
I tried this and I feel like it's really close to working. But it's not quite. I must clarify. the "#myLink" in your code above is an anchor tag surrounding the "old image". so you click on the static image, and its replaced by the live webcam. feed, which is webcam/image.jpg...basically "newImg.jpg" is this webcam image. Then after 15 seconds the original "old image" static image is there again, which you can click on again...etc....does that make sense?
Ryan Max
Well, as long as your `Refresh()` function is working properly, this would seem correct. I'll add one line to the bottom of the `click` event. That is `return false;`, so that the anchor doesn't try to load a page. Is there something specific that doesn't work here? Is your `Refresh()` getting called every 0.5 seconds?
patrick dw
+1  A: 

So something like:

var original_url = '';
var second_url = 'http://example.com/path/to/image.jpg';
var delay_in_milliseconds = 15000

function resetImage() {
    $('#myImg').attr('src', original_url);
}

$(document).ready(function () {
    original_url = $('#myImg').attr('src');
    $('#myImg').attr('src', second_url);
    setTimeout(resetImage, delay_in_milliseconds)
});

Assuming this markup:

<img id="myImg" src="another/path/to/another/image.jpg" />

I think I'm understanding your question, but feel free to revise it if you think it needs it.

artlung
the problem i'm having is that my above (in my question) javascript is running when the original image is displayed, but I only want it running while the 15 second new image is displayed.
Ryan Max
I'm not really understanding the order and frequency with which your events need to happen. Confusing. :-(
artlung
i'm sorry, I'm explaining badly. This is what I need to happen, step by step1) User comes to the page and there is a static image that says "Click here to view webcam"2) User clicks image3) Image is replaced by live webcam image, which is refreshed every .5 seconds by the second script in my question. 4) After 15 seconds the live webcam reverts back to the static image saying "click here to view webcam"It is ONLY during that 15 second interval that I wan the webcam refresh script running, otherwise it's wasting bandwidth on an element that isn't even shown. Sorry for the confusi
Ryan Max