• --:)欢迎访问锋网源码(:--
  • 首页
  • RSS订阅
  • 常用软件
  • 网页模板
  • 网站运作
  • 锋网学院
  • 智能建站
  • 时代互联
  • 中国红网
  • 中资源!
  • 繁體中文

  • 学院首页
  • 新闻资讯
  • 网站运营
  • 网站开发
  • 美工设计
  • 数据库类
  • 服务器类
  • 网络应用
  • 操作系统
  • 软件教学
编程开发   认证考试   网络安全   文章搜索: 高级搜索
会员登录/控制面版 您的位置: 学院首页 >> 网站开发 >> JavaScript >> 文章内容
 

精彩推荐

 
 

本类推荐文章

 
 

本类阅读排行

  • 网页常用特效整理:初级篇
  • 学习Ajax教程,详细了解Get与..
  • 网页常用特效整理:高级篇
  • 网页图片特效小技巧
  • 网页常用特效整理:中级篇
  • AS常用代码集锦
  • 用鼠标控制滚动的菜单条!(J..
  • 用js写的一个跑马灯
  • AS常用代码集锦
  • 在网页中控制wmplayer播放器
  • ajax中文乱码解决方法
  • XMLHTTPRequest的属性和方法..
  • 关于Ajax responseText 的一..
  • 理解JavaScript函数
  • Javascript 编程规范
  • 加快图片显示(JavaScript)
  • 用js实现select对div的隐藏与..
  • 用javascript的正则表达式来..
  • 显示客户端页面执行时间的代..
  • AJAX无刷新更新数据
 
 

Javascript教程:关于内存泄漏问题

  • 日期:2008-04-14     人气:     出处:蓝色理想     作者:
  • 字体大小:
  • 小
  • 中
  • 大

http://talideon.com/weblog/2005/03/js-memory-leaks.cfm 一文中的方法类似: Webjx.Com

/*
* EventManager.js
* by Keith Gaughan
*
* This allows event handlers to be registered unobtrusively, and cleans
* them up on unload to prevent memory leaks.
*
* Copyright (c) Keith Gaughan, 2005.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* (CPL) which accompanies this distribution, and is available at
* http://www.opensource.org/licenses/cpl.php
*
* This software is covered by a modified version of the Common Public License
* (CPL), where Keith Gaughan is the Agreement Steward, and the licensing
* agreement is covered by the laws of the Republic of Ireland.
*/
//  For implementations that don't include the push() methods for arrays.
if(!Array.prototype.push){
    Array.prototype.push=function(elem){
         this[this.length]=elem;


    }
}
var  EventManager={
    _registry: null,
    Initialise: function(){
         if(this._registry==null){
             this._registry=[];
             //  Register the cleanup handler on page unload.
             EventManager.Add(window,"unload" ,this.CleanUp);
         }
    },
    /*
     * Registers an event and handler with the manager.
     *
     * @param  obj         Object handler will be attached to.
     * @param  type        Name of event handler responds to. Webjx.Com
     * @param  fn          Handler function.
     * @param  useCapture  Use event capture. False by default.
     *                     If you don't understand this, ignore it.
     *
     * @return True if handler registered, else false.
     */
    Add: function(obj, type, fn, useCapture){
         this.Initialise();
         //  If a string was passed in, it's an id.
         if(typeof obj=="string"){
            obj = document.getElementById(obj);
         }
         if(obj==null || fn==null){
            return  false ;
         }
         // Mozilla/W3C listeners?
         if(obj.addEventListener){
            obj.addEventListener(type, fn, useCapture);
            this._registry.push({obj: obj, type: type, fn: fn, useCapture: useCapture});
            return  true ;
         }
         //  IE-style listeners?
         if(obj.attachEvent && obj.attachEvent("on" + type,fn)){
            this._registry.push({obj: obj, type: type, fn: fn, useCapture: false }); Webjx.Com
            return true ;
         }
         return false ;
    },
    /* *
     * Cleans up all the registered event handlers.
     */
    CleanUp: function(){
         for(var i=0;i<EventManager._registry.length;i++){
             with(EventManager._registry[i]) {
                 // Mozilla/W3C listeners?
                 if(obj.removeEventListener) {
                    obj.removeEventListener(type, fn, useCapture);
                 }
                 else if(obj.detachEvent){//  IE-style listeners?
                    obj.detachEvent("on"+type,fn);
                 }
             }
         }
         //  Kill off the registry itself to get rid of the last remaining
         //  references.
         EventManager._registry = null ;
    }
};

Webjx.Com

使用起来也很简单:

网页教学网

<html>
<head>
<script type=text/javascript src=EventManager.js></script>
<script type=text/javascript>   
function onLoad(){   
   EventManager.Add(document.getElementById(testCase),click,hit);
   return true;   
}   
function hit(evt) {       
   alert(click);   
}
</script>
</head>
<body onload='javascript: onLoad();'>
<div id='testCase' style='width:100%; height: 100%; background-color: yellow;'> 
<h1>Click me!</h1>
</div>
</body>
</html>

google map api同样提供了一个类似的函数用在页面的unload事件中,解决Closure带来的内存泄露问题。

Webjx.Com

当然,如果你不嫌麻烦,你也可以为每个和native object有关的就阿vascript object编写一个destoryMemory函数,用来手动调用,从而手动解除Dom对象的事件绑定。

还有一种就是不要那么OO,抛弃Dom的一些特性,用innerHTML代替appendChild,避开循环引用。详细见http://birdshome.cnblogs.com/archive/2005/02/16/104967.html中的讨论贴。

Webjx.Com

[1] [2] [3]
相关文章
  • Javascript教程:实用的JS函数库
  • JavaScript教程:onmouseover控制图片
  • 用javascript来实现动画导航
  • Javascript 编程规范
  • Javascript实例教程(1) 创建弹出式窗口
  • Javascript实例教程(2) 创建折叠式导航菜单
  • Javascript实例教程(4) 探测浏览器插件
  • Javascript实例教程(3) 探测浏览器插件
  • Javascript实例教程(5) 在一个表单中设置和检查Cookie..
  • Javascript实例教程(6) 利用Javascript进行密码保护
相关软件

  • 网友评论:
  • 查看所有评论
  • 我要发表评论
 

关于本站 | 广告联系 | 版权声明 | 网站地图 | 加入收藏 | 帮助中心 |

Copyright © 2006-2007 fwvv.net  程序支持:木翼  皖ICP备06004916号  

感谢:点击网络 联网科技 天盈信息  提供服务器及带宽赞助