Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
My interval is not continuing after gets called second time

enter image description here

what i tried

let intervalTimer = null;
function startTimer(callback, interval) {
    // Write the code that goes here
    intervalTimer = setInterval(() => {
        let obj = {number:0};
        if(callback(obj)) {

        } else {

        }
    },interval);
}
  
function callback(obj) {
obj.number++
console.log(obj.number);
return obj.number < 5;
}

this is my code so far - but i only manage to get 1. After that the eecution is not continuing. How can i solve this ?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Place the let obj = {number:0}; outside the function closure, just like you did with the let intervalTimer. Because your adding zero to the callback, its gets incremented by 1,then it gets set back to 0.

about 4 years ago · Juan Pablo Isaza Relatório

0

In the original, obj is being recreated each time the interval fires, hence it never advances.

This also returns the interval ID to the caller, allowing for multiple timers as per the requirement.

function startTimer(callback, interval) {

    let obj = {number:0};

    const intervalTimer = setInterval(() => {
        if(!callback(obj)) {
            clearInterval(intervalTimer)
        }
    },interval)

    return intervalTimer
}

function callback(obj) {
    obj.number++
    console.log(obj.number)
    return obj.number < 5
}

const timer1 = startTimer(callback, 500)
const timer2 = startTimer(callback, 750)

about 4 years ago · Juan Pablo Isaza Relatório

0

The following demonstrates the concurrent running of two timers. Each timer callback will log a value based on its counter variable. The first timer operates on an interval of 500ms, and the second, 1000ms.

Note the use of the logical OR operator (||):

  • If the left-hand side of the operator evaluates to a truthy value (ie. we should continue), then the right-hand side (timer cancellation) is not evaluated.

  • If the left-hand side of the operator evaluates to a falsy value, then the right-hand side (the cancellation) is evaluated, and the timer stops.

const startTimer = (callback, interval = 1000) => {
    let counter = 1
    const tick = () => 
        (callback(counter++) || clearInterval(intervalId))
    const intervalId = setInterval(tick, interval)    
}

const countToTen = (counter) => {    
    console.log(counter)
    return counter < 10
}

const abcs = (counter) => {    
    // 65 is the start of the Latin Alphabet
    console.log(String.fromCharCode(64 + counter)) 
    return counter < 10
}

startTimer(countToTen, 500)
startTimer(abcs)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda