What is the best way to implement a spinlock in JavaScript?
I am attempting to load multiple images and I need to wait until all of them have finished loading before proceeding. My current approach involves using a spinlock like this:
for(...)
image[i].onload = function() { ++imagesloaded; }
while(imagesloaded != totalimages)
{
}
However, this implementation crashes my browser. Is there a more efficient way to achieve this? Are there any alternative functions such as yield or sleep that I am overlooking?