<HTML>
<body>
</body>
<script>
function f(x) {
alert(x);
}
function delay(func, sec){
return function(){
setTimeout(()=>f.apply(this, arguments), sec);
};
//arrow function do not have this,
//f.apply takes context and arguments from delay, outter function
//that is why filling arguments like setTimeout(fun(args))
//always returned undefined
}
// create wrappers
let f1000 = delay(f, 1000);
let f1500 = delay(f, 1500);
f1000("test"); // shows "test" after 1000ms
f1500("test"); // shows "test" after 1500ms
</script>
</HTML>
<body>
</body>
<script>
function f(x) {
alert(x);
}
function delay(func, sec){
return function(){
setTimeout(()=>f.apply(this, arguments), sec);
};
//arrow function do not have this,
//f.apply takes context and arguments from delay, outter function
//that is why filling arguments like setTimeout(fun(args))
//always returned undefined
}
// create wrappers
let f1000 = delay(f, 1000);
let f1500 = delay(f, 1500);
f1000("test"); // shows "test" after 1000ms
f1500("test"); // shows "test" after 1500ms
</script>
</HTML>
댓글
댓글 쓰기