TIL(JS)

0

setTimeout

function work() {
  setTimeout(() => {
    const start = Date.now();
    for (let i = 0; i < 1000000000; i++) {}
    const end = Date.now();
    console.log(end - start + 'ms');
  }, 0);
}

console.log('작업 시작!');
work();
console.log('다음 작업');

//작업 시작
//다음 작업
//20582ms

If you write setTimeout() that is running in background, result of work() is turning out last time.

callback function

function work(callback) {
  setTimeout(() => {
    const start = Date.now();
    for (let i = 0; i < 1000000000; i++) {}
    const end = Date.now();
    console.log(end - start + 'ms');
    callback();
  }, 0);
}

console.log('작업 시작!');
work(() => {
  console.log('작업이 끝났어요!')
});
console.log('다음 작업');

But second case result isn't same. result of callback() is last.

profile
I want to be digital nomad!

1개의 댓글

comment-user-thumbnail
2023년 3월 20일

When I tried on my vest, it looked good; it almost fit, which is the most crucial factor for me.
https://golden-corral-menu-prices.info/

답글 달기