简体   繁体   中英

Javascript setInterval not running in Safari on windows, works in Chrome

So i am getting to know Canvas element and i made simple Canvas based gallery to get me started. I use setInterval to update src of my Image and than onLoad to draw it in canvas. Problem is that it works in Chrome but not in Safari or IE.

var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var img = new Image();
img.src = "http://lorempixel.com/g/400/400";
img.onload = function() {
    console.log('new image time');
    context.drawImage(img, 0, 0);
};
setInterval(function(){
    img.src = "http://lorempixel.com/g/400/400";
}, 4000);`

I made a fiddle: http://jsfiddle.net/lukaMis/T92TW/3/

So why doesn't setInterval run in Safari?

So

I found out it was catching of the browser problem. Browser catche request for img.src and it did not work. If if append timestamp to request it works:

_time = Date.now();
img.src = "http://lorempixel.com/g/400/400" + "?" + _time;

fiddle: http://jsfiddle.net/T92TW/5

Luka

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM