I have an array of picture URLs and I need to show each picture for an exact amount of time (750 ms). I have implemented the page using setTimeout function and it works fine except that the time i want to display each picture grows to 752ms or even more...
According to this post and many others, I have created a new function:
function getTimestamp()
{
return window.performance.now();
}
function waitFor( time, nextFunction )
{
var t1 = getTimestamp();
console.log("Start Time: " + t1);
var t2 = getTimestamp();
while ( t2 - t1 <= time)
{
t2 = getTimestamp();
}
console.log("End Time: " + t2);
nextFunction();
}
I know that javascript is excuted in a single thread and that if i run a wait function implemented using a while statement, the main thread of the page will be frozen. In other words, my function is executed synchronous. In my case, that "frosen" time is used to present the pictures, so it's fine by me.
I am changing the image source as following:
var url = "images/Practice" + imagesArr[randomNum] +".png";
console.log("image path: " + url);
imagesArr.splice(randomNum, 1);
pictureImg.src = url;
pictureImg.style.display="block";
Despite the fact that my code changes the image source perfectly, sometimes the browser won't change the image. It displays only the first picture that was displayed and sometimes it mixes up the order.
My page is here.
Importantly, my website will be running only in Chrome.
I am really puzzled here and i would appreciate any help!!!
Thank you, Max.