Last active
June 12, 2017 09:33
-
-
Save mrxf/039aeb826d70d4cac14f337911c08cce to your computer and use it in GitHub Desktop.
@decorator 装饰器
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 控制一个函数延后执行 | |
* @param tick 休眠时间 | |
*/ | |
function sleep(tick: number){ | |
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => { | |
let method = descriptor.value; | |
descriptor.value = (...args: any[]) =>{ | |
setTimeout(()=>{ | |
method.apply(target, args) | |
}, tick); | |
} | |
} | |
} | |
class Wait{ | |
constructor(){} | |
@sleep(3000) | |
do(yell: string){ | |
console.log("我终于被执行了" + yell); | |
console.log(new Date().getSeconds()); | |
} | |
} | |
const waiter: Wait = new Wait(); | |
/** 记录函数执行的开始时间 */ | |
console.log("Start" + new Date().getSeconds()) | |
waiter.do("欢呼"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment