描述我的问题
js的回调函数+setTImeout(或者视频/音频的ended事件)实现能控制时间间隔的各种素材的轮播,比如图片播10s切换到视频,视频播完可能切换到文字显示,文字显示30s又从头播起。......这样就成了愚公移山,“子子孙孙无穷无尽”。然后,问题就来了,程序一直回调,发现内存没有被释放,假如放的视频是无码超清200M的,可能几轮过后,就显示视频解码器狗带!究竟要怎样才能在回调的同时释放内存,或者不要回调实现这个坑爹的轮播。关键是特么页面至少有三处这个的“死亡回调”
贴上我问题的相关代码(有用吗?)
//video
! function (global) {
var videoPlayer = {
initialize: function (data, callback) {
/**
* data : {
* aid : '' ,
* itemIndex : 0 ,
* itemData : {
*
* }
* }
*/
console.log('This is videoPlayer module !');
new VideoPlayer(data, callback);
}
};
function VideoPlayer(data, callback) {
//.....
//此处省略添加video的代码
//视频播完回调
$('#' + this.data.aid + ' video')[0].onended = function () {
console.log("a video ended")
//此处应是 “死亡回调”
$this.callback && $this.callback({
aid: $this.data.aid,
itemIndex: $this.data.itemIndex
});
};
}
global.mediaplayer = videoPlayer;
}(window.TMC);
//image
! function (global) {
var imagePlayer= {
initialize: function (data, callback) {
/**
* data : {
* aid : '' ,
* itemIndex : 0 ,
* itemData : {
*
* }
* }
*/
console.log('This is imagePlayermodule !');
new imagePlayer(data, callback);
}
};
function imagePlayer(data, callback) {
//.....
//此处省略添加img的代码
//图片播完回调
timer = setTimeout(function () {
clearTimeout(timer );
// 此处也是 “死亡回调”
$this.callback && $this.callback({
aid: $this.data.aid,
itemIndex: $this.data.itemIndex
});
}, $this.data.itemData.len * 1000);
}
global.imageplayer= imagePlayer;
}(window.TMC);
3.4. 贴上报错信息和截图


Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
就是这个机制啊。如果你写成这种结构,就只能这样了。不过ES6有个尾调用优化,可以解决这个问题。虽然我更推荐楼主别这么写。
感觉你不管video还是image都应该试试用单例模式来写,这样每次用户不管是调video还是image,内存里只保证有一个实例在就好。