Why not to use setInterval - Lee-hyuna/33-js-concepts-kr GitHub Wiki

์™œ setInterval์„ ์“ฐ๋ฉด ์•ˆ๋˜๋Š”๊ฐ€

์ตœ๊ทผ์— ์ €๋Š” 10์ดˆ๋งˆ๋‹ค ajax์„ ๋ณด๋‚ด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ํŠน์ •ํ•œ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ ํ›„์— ๋ฐ˜๋ณต์ ์œผ๋กœ ๊ธฐ๋Šฅ์— call ํ•˜๋Š” ์š”๊ตฌ ์กฐ๊ฑด์„ ๋ฐœ๊ฒฌํ–ˆ์–ด์š”. ์ตœ์ƒ์˜ ์˜ต์…˜์€ ์„ค์ •๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. setInterval์ด ๋‚ด ์–ผ๊ตด์„ ํฌ๋ž˜์ปค์ฒ˜๋Ÿผ ๋‚ ๋ ค๋ฒ„๋ ธ์–ด์š” :)

setInterval์ด ์™œ ์•…ํ•œ์ง€ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ ์šฐ๋ฆฌ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋ณธ์งˆ์ ์œผ๋กœ single threaded๋ผ๋Š” ์‚ฌ์‹ค์„ ๋ช…์‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, ํ•œ ๋ฒˆ์— ๋‘ ๊ฐœ ์ด์ƒ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

function์ด setInterval ์—์„œ ์–ธ๊ธ‰๋œ ์ง€์—ฐ ์‹œ๊ฐ„๋ณด๋‹ค ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๊ฒฝ์šฐ(์˜ˆ: ์ œ๋•Œ ์™„๋ฃŒ๋˜์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ๋Š” ajax call), breathing room์ด ์—†๊ฑฐ๋‚˜ setInterval์ด ๋ฆฌ๋“ฌ์„ ๊นจ๋œจ๋ฆฝ๋‹ˆ๋‹ค.

    var fakeCallToServer = function() {
        setTimeout(function() {
            console.log('returning from server', new Date().toLocaleTimeString());
        }, 4000);
    }



    setInterval(function(){ 

        let insideSetInterval = new Date().toLocaleTimeString();

        console.log('insideSetInterval', insideSetInterval);

        fakeCallToServer();
    }, 2000);

//insideSetInterval 14:13:47
//insideSetInterval 14:13:49
//insideSetInterval 14:13:51
//returning from server 14:13:51
//insideSetInterval 14:13:53
//returning from server 14:13:53 
//insideSetInterval 14:13:55
//returning from server 14:13:55

Try above code snippets in your console

console.log ๋ฌธ์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. setInterval ์€ ajax call์ด ์˜ค๋“  ์•ˆ ์˜ค๋“  ์ƒ๊ด€ํ•˜์ง€ ์•Š๊ณ  ๊ณ„์†ํ•ด์„œ ajax call์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์„œ๋ฒ„์—์„œ ๋งŽ์€ ์š”์ฒญ์„ ํ•œ ๋ฒˆ์— ๋Œ€๊ธฐ์—ด์— ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์ œ setInterval์—์„œ ๋™๊ธฐ์‹ ์—ฐ์‚ฐ์„ ์‹œ๋„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

var counter = 0;

var fakeTimeIntensiveOperation = function() {

    for(var i =0; i< 50000000; i++) {
        document.getElementById('random');
    }

    let insideTimeTakingFunction  = new Date().toLocaleTimeString();

    console.log('insideTimeTakingFunction', insideTimeTakingFunction);
}



var timer = setInterval(function(){ 

    let insideSetInterval = new Date().toLocaleTimeString();

    console.log('insideSetInterval', insideSetInterval);

    counter++;
    if(counter == 1){
        fakeTimeIntensiveOperation();
    }

    if (counter >= 5) {
       clearInterval(timer);
    }
}, 1000);

//insideSetInterval 13:50:53
//insideTimeTakingFunction 13:50:55
//insideSetInterval 13:50:55 <---- not called after 1s
//insideSetInterval 13:50:56
//insideSetInterval 13:50:57
//insideSetInterval 13:50:58

setInterval์ด ์‹œ๊ฐ„ ์ง‘์ค‘์ ์ธ ์šด์šฉ์„ ๊ฒฝํ—˜ํ•  ๋•Œ,

a) ํŠธ๋ž™์„ ์˜ค๋ฅด๋ ค๊ณ  ํ•˜๊ฑฐ๋‚˜

b) ์ƒˆ๋กœ์šด ๋ฆฌ๋“ฌ์„ ์ฐฝ์กฐํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ ํฌ๋กฌ์€ ์ƒˆ๋กœ์šด ๋ฆฌ๋“ฌ์„ ๋งŒ๋“ค์–ด๋ƒ…๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

๋น„๋™๊ธฐ์‹ ์ž‘์—…์˜ ๊ฒฝ์šฐ setTimeInterval์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๊ฒฉ์€ ์š”์ฒญ์˜ ๊ธด ๋Œ€๊ธฐ์—ด์„ ์ƒ์„ฑํ•˜์—ฌ ๋งค์šฐ ์—ญํšจ๊ณผ๋ฅผ ๋ƒ…๋‹ˆ๋‹ค.

์‹œ๊ฐ„ ์ง‘์•ฝ์ ์ธ ๋™๊ธฐํ™” ์ž‘์—…์˜ ๊ฒฝ์šฐ setTimeInterval์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๊ฒฉ์ด ๋ฆฌ๋“ฌ์„ ๊นจ๋œจ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ setInterval ์ฝ”๋“œ ๋ธ”๋ก์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์‹คํ–‰์„ ์ค‘์ง€ํ•˜์ง€ ์•Š๊ณ  ์ž˜๋ชป๋œ ์ฝ”๋“œ๋ฅผ ๊ณ„์† ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๋งํ•  ๊ฒƒ๋„ ์—†์ด ๊ทธ๋“ค์€ ๋งํ•  ๊ฒƒ๋„ ์—†๊ณ  clearInterval(๊ฐ„๊ฒฉ) ๊ธฐ๋Šฅ์œผ๋กœ ์ค‘์ง€ํ•ฉ๋‹ˆ๋‹ค.

๋˜๋Š” ์‹œ๊ฐ„์— ๋ฏผ๊ฐํ•œ ์ž‘์—…์˜ ๊ฒฝ์šฐ setTimeout์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.