Correction for stack overflow when many images in one page

replace recursive call by loop.
call  callback with a setTimeout()