JavaScript设置超时内存问题
我正在调试一个我认为有内存泄漏的代码。在Google Chrome中运行代码时,浏览器会在一段时间后崩溃。我坚信下面的代码(为了简单起见,只附加一个模板,而不是整个代码)导致了这个问题。当我使用ChromeDeveloperToolsTimeline选项卡,观察真实应用程序中的内存使用情况和内存峰值时。如果您注意到播放正在调用Transition的代码,Transistito有一个setTimeout函数再次调用播放。我认为内存没有因此而释放。谁能帮我解决这个问题JavaScript设置超时内存问题,javascript,memory,settimeout,Javascript,Memory,Settimeout,我正在调试一个我认为有内存泄漏的代码。在Google Chrome中运行代码时,浏览器会在一段时间后崩溃。我坚信下面的代码(为了简单起见,只附加一个模板,而不是整个代码)导致了这个问题。当我使用ChromeDeveloperToolsTimeline选项卡,观察真实应用程序中的内存使用情况和内存峰值时。如果您注意到播放正在调用Transition的代码,Transistito有一个setTimeout函数再次调用播放。我认为内存没有因此而释放。谁能帮我解决这个问题 var htmlRot
var htmlRotatorTimer = '';
function play(src){
// some code here
transitionTo("gallery", "counter");
// some code here
}
function transitionTo(gallery,index) {
// some code here
clearTimeout(htmlRotatorTimer);
htmlRotatorTimer = setTimeout( function(){ play(); }, 1000 );
// some code here
}
play();
下面是代码的较长版本
// utility for loading slides
function transitionTo(gallery,index) {
// preloader (optional)
var counterMin = 0;
if (config.preloader) {
counterMin = 1;
if (gallery.length < 3) pause('');
}
var oldCounter = counter;
if((counter >= gallery.length) || (index >= gallery.length)) { counter = counterMin; var e2b = true; }
else if((counter < counterMin) || (index < 0)) { counter = gallery.length-1; var b2e = true; }
else { counter = index; }
// added attr speed in div's
//htmlRotatorTimer = '';
var itemSpeed = Number($(gallery[counter]).attr("speed") != undefined ? $(gallery[counter]).attr("speed") : config.speed);
//htmlRotatorTimer = setInterval(function(){ play(); },itemSpeed);
clearTimeout(htmlRotatorTimer);
htmlRotatorTimer = setTimeout( function(){ play(); }, itemSpeed );
var rmkName = $(gallery[counter].children).attr("id") != undefined ? 'RMK_' + $(gallery[counter].children).attr("id") : '';
var isHtml5 = false;
if (rmkName != '' && eval ('typeof '+ rmkName) == 'object') {
rmkObj = eval(rmkName);
isHtml5 = rmkObj.rmkType == 'html5' ? true : false;
//console.log('html5 is' + rmkObj.rmkType,'obj name' + rmkName, 'typeof:' +(typeof rmkObj));
}
if (config.effect=='fade') {
$(gallery[counter])
.clone()
.appendTo($cont)
.hide()
.fadeIn(config.changeSpeed,function(){$('#showbanners.rmkbanner').css({ 'visibility': 'visible'});if($.browser.msie)this.style.removeAttribute('filter');});
if($cont.children().length>1){
$cont.children().eq(0).css('position','absolute').fadeOut(config.changeSpeed,function(){$(this).remove();});
};
} else if (config.effect=='none') {
$(gallery[counter])
.appendTo($cont);
if($cont.children().length>1){
$cont.children().eq(0).remove();
};
};
// update active class on slide link
if(config.links){
$('.'+uniqueClass+'-active').removeClass(uniqueClass+'-active jshowoff-active');
$('.'+uniqueClass+'-slidelinks a').eq(counter).addClass(uniqueClass+'-active jshowoff-active');
};
// reset for html5 objects only
if (isHtml5) {
rmkObj.preload = 'nopreload';
rmkObj.Reset();
}
};// end function transistionTo
// is the rotator currently in 'play' mode
function isPlaying(){
return $('.'+uniqueClass+'-play').hasClass('jshowoff-paused') ? false : true;
};
// start slide rotation on specified interval
function play(src) {
if (!finalStop) {
if (!isBusy()) {
counter++;
transitionTo(gallery, counter);
if (src == 'hover' || !isPlaying()) {
//htmlRotatorTimer = setInterval(function(){ play(); },config.speed);
clearTimeout(htmlRotatorTimer);
htmlRotatorTimer = setTimeout(function(){
play();
}, config.speed);
}
if (!isPlaying()) {
$('.' + uniqueClass + '-play').text(config.controlText.pause).removeClass('jshowoff-paused ' + uniqueClass + '-paused');
}
};
};
};
//用于加载幻灯片的实用程序
功能转换(库、索引){
//预加载程序(可选)
var counterMin=0;
if(config.preload){
counterMin=1;
如果(画廊长度<3)暂停(“”);
}
var oldCounter=计数器;
如果((计数器>=gallery.length)| |(索引>=gallery.length)){counter=counterMin;var e2b=true;}
else如果((counter1){
$cont.children().eq(0).css('position','absolute').fadeOut(config.changeSpeed,function(){$(this.remove();});
};
}else if(config.effect='none'){
$(画廊[柜台])
.附件(续);
如果($cont.children().length>1){
$cont.children().eq(0).remove();
};
};
//更新幻灯片链接上的活动类
if(config.links){
$('.+uniqueClass+'-active').removeClass(uniqueClass+'-activejshowoff-active');
$('.+uniqueClass+'-slidelinks a').eq(counter).addClass(uniqueClass+'-activejshowoff-active');
};
//仅针对html5对象重置
如果(isHtml5){
rmkObj.preload='nopreload';
rmkObj.Reset();
}
};// 端功能转换
//旋转器当前是否处于“播放”模式
函数isPlaying(){
返回$('.+uniqueClass+'-play').hasClass('jshowoff-paused')?false:true;
};
//按指定的时间间隔开始幻灯片旋转
功能播放(src){
如果(!finalStop){
如果(!isBusy()){
计数器++;
中转站(走廊、柜台);
如果(src==“悬停”| |!isplay()){
//htmlRotatorTimer=setInterval(函数(){play();},config.speed);
clearTimeout(HTMLRotatTimer);
htmlRotatorTimer=setTimeout(函数(){
play();
},配置速度);
}
如果(!isPlaying()){
$('.+uniqueClass+'-play').text(config.controlText.pause).removeClass('jshowoff-paused'+uniqueClass+'-paused');
}
};
};
};
我认为您对play的调用在Transitiono函数中是错误的。你没有提供论点。顺便问一下,为什么还要在外部调用play()?如果可能,请张贴完整的代码。我不知道为什么要将计数器字符串传递给Transition。一个建议是在play方法中使用setInterval方法,如
function play(){
//existing logic
setInterval(function(){transitionTo("gallery","counter");},1000);
}
这里的其他代码是什么,您列为“此处的某些代码”的内容?此代码不会导致高内存使用率(更不用说内存泄漏)。config.changeSpeed设置为什么?如果它超过了config.speed,那么最终肯定会成为一个问题。此外,您还需要每秒执行大量未缓存的DOM属性检索/更新,例如,您是否考虑过缓存一些元素名称,以便不必每秒钟搜索它们?config.changeSpeed值为600,config.speed设置为12000。这段代码实际上是一个名为jShowOff的jquery插件,我所在的开发人员对其进行了修改。该插件使用setInterval,也没有从Transiston播放到函数的调用。在大多数情况下,使用
setInterval
而不是setTimeout
总是一个坏主意。