← 返回首页
What will setTimeout show?
EN

We want to make this open-source project available for people all around the world.

Help to translate the content of this tutorial to your language!

    Search
    Search
    Light themeDark theme
    عربيDanskEnglishEspañolفارسیFrançaisIndonesiaItaliano日本語한국어РусскийTürkçeУкраїнськаOʻzbek简体中文
    back to the lesson

    What will setTimeout show?

    importance: 5

    In the code below there’s a setTimeout call scheduled, then a heavy calculation is run, that takes more than 100ms to finish.

    When will the scheduled function run?

    1. After the loop.
    2. Before the loop.
    3. In the beginning of the loop.

    What is alert going to show?

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

    Any setTimeout will run only after the current code has finished.

    The i will be the last one: 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++; }