Note that there are some explanatory texts on larger screens.

plurals
  1. POSmoothly replace image when reloading with javascript
    primarykey
    data
    text
    <p>I am replacing an image on a page on a timer using setInterval in javascript (it's a stats graph that needs to be up to date). </p> <p>I am using the following code:</p> <pre><code>var tid = setInterval(mycode, 20000); function mycode() { var theDate = new Date(); var mili = theDate.getUTCDate() + theDate.toLocaleTimeString() + theDate.getMilliseconds(); var img = $('&lt;img /&gt;') .attr('src', '@Url.Action("Chart", "Home", new {ForceNoCache = "theDate"})') .attr('id', 'GraphImage') .load(function () { if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) { alert('broken image!'); } else { $('#GraphImage').replaceWith(img); } }); img.attr('src', img.attr('src').replace("theDate", mili)); } </code></pre> <p>The image is currently just sitting in a div on the page like this:</p> <pre><code> &lt;div style="float:left;margin-right:20px"&gt; &lt;img id="GraphImage" alt="Graph of Results" src="@Url.Action("ChartImage", "Home")" /&gt; &lt;/div&gt; </code></pre> <p>This code works and replaces the image every 20 seconds - however, even though I'm using the .load function and not replacing the image until it is fully loaded, I still get an annoying little flicker as the browser swaps the image over.</p> <p>How would I go about using a jQuery fade transition/animation to smoothly swap over the two images? Ideally I'd like a way to do this without needing too much in the way of additional markup gumph or css limitations on how the image can be styled and positioned in the page.</p> <p>Similar to this question: <a href="https://stackoverflow.com/questions/5098276/javascript-image-reloading-flickers">Javascript Image Reloading; flickers</a></p> <p>However I am already using the accepted answer on this question, and still getting flicker.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload