가자미의 개발이야기
[JS] 비동기 작업을 다루는 setTimeout() 본문
setTimeout : ms초 뒤에 백그라운드에서 작업할 콜백을 정의
//비동기 작업 다루기 setTimeout
function work(callback){
setTimeout(()=>{//백그라운드에서 작업실행
const start = Data.now();
for(let i=0;i<100000;i++){
}
const end =Data.now();
console.log(end-start+'ms');
callback(end-start);//매개변수로 함수를 받아서 마지막을로 작업실행
}, 0)
}
console.log('작업시작');
work((ms)=>{
console.log('작업이 끝났어요');
console.log(ms+'ms');
});
console.log('다음작업');
콜백을 매개변수로 받는 work 함수.
work함수는 setTimeout으로 반복문 작업을 하고 매개변수 콜백을 마지막으로 실행.
'HTML & CSS & JS > JS' 카테고리의 다른 글
[JS] async 와 await (0) | 2021.03.17 |
---|---|
[JS] 많은 콜백함수를 정리하는 promise (0) | 2021.03.17 |
[JS] spread, rest (0) | 2021.03.17 |
[JS] 비구조할당 (0) | 2021.03.17 |
[JS] falthy , truthy (0) | 2021.03.17 |