prototype.js - IE8 - recursive function doesn't work after refresh
I have this recursive function, where thumbs is array of IMG objects... Whole function works properly for me in Chrome, IE9, FF and so on, but freezes in IE8 after refreshing or navigating from site to site... When new tab is opened, all IMGs loads correctly, but after reload script "dies" without any sings of error... The function is called from some 'init' function where thumbs array, filename etc. is to be initiated... appreciate your time guys...
function preload(thumbs) {
last = thumbs[thumbs.length - 1];
loadThumb(0);
function loadThumb(th) {
filename = thumbs[th].id.spl开发者_Python百科it('-')[1];
thumbs[th].setAttribute('src', '/data/cache/thumb-' + filename + '.jpg');
handle(th, thumbs);
thumbs[th].observe('load', (function (event) {
thumbs[th].setStyle({
visibility: 'visible',
opacity: 0.3
});
loadThumb(th + 1);
}));
}
};
last.observe('load', (function (event) {
$('load').setStyle({
visibility: 'hidden'
});
kar_width = last.positionedOffset();
$('karusel').setStyle({
width: kar_width[0] + last.getWidth() + 10 + 'px'
});
if ($('karusel').getWidth() < 700) {
$('next').hide();
$('prev').hide();
$('next1').hide();
$('prev1').hide();
};
}));
};
I doubt this will solve your problem, but it's usually not a good idea to use setAttribute
to set the source of an image element, IE (esp. < 9) might choke on that. Use direct DOM element properties instead:
thumbs[th].src = '/data/cache/thumb-' + filename + '.jpg';
Update: A stack overflow error in combination with a recursive function usually means that you're in an endless loop of recursion. In this code:
function loadThumb(th) {
/* ... */
thumbs[th].observe('load', (function (event) {
/* ... */
loadThumb(th + 1);
}));
}
you never break out of this recursive function.
I suggest you add a condition to get out of all loadThumb
instances, e.g.:
if (th < maxThumbs)
loadThumb(th + 1);
精彩评论