定时器的单利模式

22 篇文章 0 订阅
12 篇文章 0 订阅

主要代码

用的时候看使用说明 不懂滴留言(求求关注)


var HashMap = require('../tools/HashMap')
/**
 * 使用说明:
 * 1、引入 var  timeTool=require("../utils/timeTool.js")
 * 2、onload 里面实例化并调用start方法:
 *       mtimeTool = new timeTool( this);      mtimeTool.start();

添加监听函数:keykey随意写不要重复就好
 mPKGame.addCallBack("keykey", () => {})
 */
//

class PKGame {
	constructor(handler) {
		this.mNetTool = netTool;
		this.mHandler = handler;
		this.mHandler;
		this.commonTimer;
		this.callBackListener = new HashMap();
		this.instance;
		// uni.setStorageSync("token",handler.appInfo.token)
	}

	destroy() {
		this.clearInterval(commonTimer)
		this.commonTimer = null;
	}
	start() {
		if (!this.commonTimer) {
			this.commonTimer = setInterval(() => {
				var values = this.callBackListener.values();
				for (var i in values) {
					typeof values[i] == "function" && values[i]();
				}
			}, 1000);
		}
		// this.createRoom();
	}
	
	addCallBack(listenerKey, listener) {
		this.callBackListener.put(listenerKey, listener)
	}
	removeCallBack(listenerKey) {
		this.callBackListener.remove(listenerKey)
	}

	static getInstance = function (handler) { //静态方法
    return this.instance || (this.instance = new PKGame(handler))
  }
}

module.exports = ( handler) => {
	return PKGame.getInstance( handler)
};

hashmap工具类

/** 
* *********  操作实例  ************** 
*   var map = new HashMap(); 
*   map.put("key1","Value1"); 
*   map.put("key2","Value2"); 
*   map.put("key3","Value3"); 
*   map.put("key4","Value4"); 
*   map.put("key5","Value5"); 
*   alert("size:"+map.size()+" key1:"+map.get("key1")); 
*   map.remove("key1"); 
*   map.put("key3","newValue"); 
*   var values = map.values(); 
*   for(var i in values){ 
*       document.write(i+":"+values[i]+"   "); 
*   } 
*   document.write("<br>"); 
*   var keySet = map.keySet(); 
*   for(var i in keySet){ 
*       document.write(i+":"+keySet[i]+"  "); 
*   } 
*   alert(map.isEmpty()); 
*/  
  
function HashMap(){  
  //定义长度  
  var length = 0;  
  //创建一个对象  
  var obj = new Object();  

  /** 
  * 判断Map是否为空 
  */  
  this.isEmpty = function(){  
      return length == 0;  
  };  

  /** 
  * 判断对象中是否包含给定Key 
  */  
  this.containsKey=function(key){  
      return (key in obj);  
  };  

  /** 
  * 判断对象中是否包含给定的Value 
  */  
  this.containsValue=function(value){  
      for(var key in obj){  
          if(obj[key] == value){  
              return true;  
          }  
      }  
      return false;  
  };  

  /** 
  *向map中添加数据 
  */  
  this.put=function(key,value){  
      if(!this.containsKey(key)){  
          length++;  
      }  
      obj[key] = value;  
  };  

  /** 
  * 根据给定的Key获得Value 
  */  
  this.get=function(key){  
      return this.containsKey(key)?obj[key]:null;  
  };  

  /** 
  * 根据给定的Key删除一个值 
  */  
  this.remove=function(key){  
      if(this.containsKey(key)&&(delete obj[key])){  
          length--;  
      }  
  };  

  /** 
  * 获得Map中的所有Value 
  */  
  this.values=function(){  
      var _values= new Array();  
      for(var key in obj){  
          _values.push(obj[key]);  
      }  
      return _values;  
  };  

  /** 
  * 获得Map中的所有Key 
  */  
  this.keySet=function(){  
      var _keys = new Array();  
      for(var key in obj){  
          _keys.push(key);  
      }  
      return _keys;  
  };  

  /** 
  * 获得Map的长度 
  */  
  this.size = function(){  
      return length;  
  };  

  /** 
  * 清空Map 
  */  
  this.clear = function(){  
      length = 0;  
      obj = new Object();  
  };  
}
module.exports = HashMap;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

栈狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值