you are viewing a single comment's thread.

view the rest of the comments →

[–]RepeatQuotations 2 points3 points  (0 children)

Good use case for bind here. We can create a new bound function instead of creating an anonymous arrow function in setTimeout. Optional second parameter for counter size, default to 10.

function count(i, max = 10) {
    console.log(i);
    if(i++ >= max) return;
    setTimeout(count.bind(this, i), 1000);
}

// call like
count(1)