1. 程式人生 > >【typescript】簡單公用方法

【typescript】簡單公用方法


/**範圍內獲取整數隨機數*/
/**範圍內獲取整數隨機數*/
function getRandomInt(min: number, max: number): number {
    var Range = max - min;
    var Rand = Math.random();
    return(min + Math.round(Rand * Range));
}


判斷Object是否為空

function isEmptyObject(obj) {
  for (var key in obj){
    return false;//返回false,不為空物件
  }  
  return true;//返回true,為空物件
}


滾動數值

	private maxNum:number = 0;
	/* 滾動數字效果。
	maxNum:結果數值,
	txtNode: eui.BitmapLabel:需要變化的節點*/
	function onNumChange(maxNum: number, txtNode: eui.BitmapLabel) {
		if(maxNum > this.maxNum) this.maxNum = maxNum;
		let nowNum = Number(txtNode.text);//原顯示數字
		nowNum += Math.ceil((maxNum-nowNum) / 10);
		// console.log(nowNum , maxNum,"___nowNum , maxNum________")
		if (nowNum < maxNum) {
			txtNode.text = nowNum.toString();
			this._changeTimeOut = setTimeout2(() => { this.onNumChange(maxNum, txtNode); }, 30);
		} else {
			txtNode.text = maxNum.toString();
			if(this.maxNum == maxNum){
				clearTimeout2(this._changeTimeOut);
				this.maxNum = 0;
			}
		}

	}

	function setTimeout2(closure: Function, delay: number, ...parameters): number {
		var si: number = setTimeout(exec, delay, closure, delay, parameters);
		function exec(func: Function, delay: number, arg: Array<any> = null): void {
			clearTimeout2(si);
			func.apply(null, arg);
			closure = null;
		}
		return si;
	}

	function clearTimeout2(si:number):void{
		clearTimeout(si);
	}