Skip to content

Instantly share code, notes, and snippets.

@mrxf
Last active June 12, 2017 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrxf/039aeb826d70d4cac14f337911c08cce to your computer and use it in GitHub Desktop.
Save mrxf/039aeb826d70d4cac14f337911c08cce to your computer and use it in GitHub Desktop.
@decorator 装饰器
/**
* 控制一个函数延后执行
* @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