Dynamic Refresh With Javascript
I'm trying to get an user input image to refresh say every two seconds. The javascript gets user input for an URL and the javscript adds it to the page. Then the images loaded need
Solution 1:
When you need to force reload the resource, you have to add a dymmy queryString at the end of your url:
<img id="img1" src="myimg.png?dummy=23423423423">
Javascript:
$('#img1').attr('src','myimg.png?dummy=23423423423');
and change the dummy value for each refresh
Solution 2:
Your premise seems good, not sure why it isn't working. Why mixing and matching jQuery with non-jquery? This is a modification of your code, but it is a working example. You should be able to adapt it for your purpose.
You can create elements via jQuery easily. See the modified getImg
function I created. Ia also switched from attr
to prop
which is the recommended means of accessing the src
attribute in jQuery 1.7+.
functiongetImg() {
var url = 'http://placehold.it/300x300/123456';
var $div = $('<div />');
var $img = $('<img />', { src: url });
$div.append($img);
$('body').append($div);
}
getImg();
var interval = setInterval(function() {
$('img').each(function() {
var time = (newDate()).getTime();
var oldurl = $(this).prop('src');
var newurl = oldurl.substring(0, oldurl.lastIndexOf('/') + 1) + time.toString().substr(7, 6);
$('#output').html(newurl);
$(this).prop('src', newurl);
});
}, 2000);
Solution 3:
Put the image in a container as:
<divid="container"><imgsrc=""../></div>
then simply update the content of the container as:
$('#container').html('<img src="newSource"../>');
Post a Comment for "Dynamic Refresh With Javascript"