[ Web Proxy ]
URL:
Viewing: https://ko.javascript.info/settimeout-setinterval [Back]  [Original]

setTimeout setInterval
:
Light themeDark theme
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
2021 12 15

setTimeout setInterval

() ' (scheduling a call)' .

.

  • setTimeout
  • setInterval

setTimeout setInterval . , Node.js .

setTimeout

:

let timerId = setTimeout(func|code, [delay], [arg1], [arg2], ...)

:

func|code
, . . .
delay
, (millisecond, 1000 = 1) 0.
arg1, arg2
, IE9 .

setTimeout . 1 sayHi() .

function sayHi() {
  alert('.');
}

setTimeout(sayHi, 1000);

.

function sayHi(who, phrase) {
  alert( who + ' , ' + phrase );
}

setTimeout(sayHi, 1000, "", "."); //  , .

setTimeout .

.

setTimeout("alert('.')", 1000);

. .

setTimeout(() => alert('.'), 1000);
.

setTimeout , () .

//  
setTimeout(sayHi(), 1000);

setTimeout sayHi() . sayHi() . undefined . setTimeout , .

clearTimeout

setTimeout ' (timer identifier)' . ( timerId) .

:

let timerId = setTimeout(...);
clearTimeout(timerId);

. .

let timerId = setTimeout(() => alert("   ."), 1000);
alert(timerId); //  

clearTimeout(timerId);
alert(timerId); //     (    null  .)

alert 2 , . . Node.js setTimeout .

, . .

HTML5 timers section .

setInterval

setInterval setTimeout .

let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...)

. , setTimeout setInterval .

clearInterval(timerId) .

2 5 .

// 2   
let timerId = setInterval(() => alert(''), 2000);

// 5  
setTimeout(() => { clearInterval(timerId); alert(''); }, 5000);
alert .

Chrome Firefox alert/confirm/prompt .

alert , alert . 2 .

setTimeout

2 .

setInterval , setTimeout .

/** setInterval      setTimeout 
let timerId = setInterval(() => alert(''), 2000);
*/

let timerId = setTimeout(function tick() {
  alert('');
  timerId = setTimeout(tick, 2000); // (*)
}, 2000);

setTimeout (*) .

setTimeout setInterval . .

5 . 10, 20, 40 .

.

let delay = 5000;

let timerId = setTimeout(function request() {
  ... ...

  if (    ) {
    //   .
    delay *= 2;
  }

  timerId = setTimeout(request, delay);

}, delay);

CPU setTimeout . .

setTimeout setInterval .

. setinterval .

let i = 1;
setInterval(function() {
  func(i++);
}, 100);

setTimeout .

let i = 1;
setTimeout(function run() {
  func(i++);
  setTimeout(run, 100);
}, 100);

, func(i++) 100 .

[]

?

setInterval func (100ms) !

func . .

func ?

func . func , .

delay , .

, setTimeout .

[]

setTimeout ( 100ms) .

.

setIntervalsetTimeout

setInterval setTimeout , . setInterval setTimeout .

//       .
setTimeout(function() {...}, 100);

setInterval , clearInterval .

. . . . .

0 setTimeout

setTimeout(func, 0) setTimeout(func) setTimeout 0 .

0 func . , .

.

'Hello 'World .

setTimeout(() => alert("World"));

alert("Hello");

'0 ' . (alert ) ' , Hello , World .

0 setTimeout , .

0 .

HTML5 . " 4 ." .

. setTimeout run . times , times .

let start = Date.now();
let times = [];

setTimeout(function run() {
  times.push(Date.now() - start); //          

  if (start + 100 < Date.now()) alert(times); //   100ms , array  
  else setTimeout(run); //   100ms   
});

//  :
// 1,1,1,1,9,15,20,24,30,35,40,45,50,55,59,64,70,75,80,85,90,95,100

. 4 9, 15, 20, 24... .

setTimeout setInterval . setInterval(f) f , 4 .

, .

, . Node.js process.nextTick setImmediate . .

  • setInterval(func, delay, ...args) setTimeout(func, delay, ...args) delay func , .
  • setIntervalsetTimeout clearIntervalclearTimeout .
  • setTimeout setInterval . .
  • 0 setTimeout(setTimeout(func, 0) setTimeout(func)) .
  • setTimeout 5 setInterval 5 , 4 . , .

.

.

  • CPU

300 1,000 . .

: 5

from to printNumbers(from, to) . .

.

  1. setInterval
  2. setTimeout

setInterval :

function printNumbers(from, to) {
  let current = from;

  let timerId = setInterval(function() {
    alert(current);
    if (current == to) {
      clearInterval(timerId);
    }
    current++;
  }, 1000);
}

// usage:
printNumbers(5, 10);

setTimeout :

function printNumbers(from, to) {
  let current = from;

  setTimeout(function go() {
    alert(current);
    if (current < to) {
      setTimeout(go, 1000);
    }
    current++;
  }, 1000);
}

// usage:
printNumbers(5, 10);

( ) 1000ms .

.

function printNumbers(from, to) {
  let current = from;

  function go() {
    alert(current);
    if (current == to) {
      clearInterval(timerId);
    }
    current++;
  }

  go();
  let timerId = setInterval(go, 1000);
}

printNumbers(5, 10);
: 5

setTimeout . 100ms .

setTimeout ?

?

let i = 0;

setTimeout(() => alert(i), 100); // ?

//      100ms    .
for(let j = 0; j < 100000000; j++) {
  i++;
}

setTimeout .

i 100000000 , 100000000 .

let i = 0;

setTimeout(() => alert(i), 100); // 100000000 .

// assume that the time to execute this function is >100ms
for(let j = 0; j < 100000000; j++) {
  i++;
}

.

Web Proxy Viewer  |  New URL  |  Original Page