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

191
Visualizações
How can i stop Interval on VueJS?

I do tryin' make a progress bar with VueJS but when this count equal 100 interval still going process. How can i stop interval when data equal 100 percent?

I used the console.log() command to check if the code is working, but it returns a wrong response.

Best Regards,

That's my code:

HTML:

  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  
  <div id="app">
    <button v-on:click="startProgress">Start Progress</button>
    <br><br>
    <div class="progress" :style="{width : progressWidth + 'px'}"></div>
  </div>

CSS:

.progress{
    background-color: pink;
  transition: all 310ms ease;
    width: 0px;
    height: 20px;
  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
}

VueJS:

new Vue({
  el: '#app',
  data: {
    progressWidth : 0
  },
  methods: {
    startProgress : function(){
        var vm = this;
        setInterval(function(){
            vm.progressWidth+=10;   
        }, 150);
    vm.progressWidth= 20 ? console.log('20') : console.log('--');
    }
  },
});

if you want you can check live on jsFidde: https://jsfiddle.net/6j05p8Lb/46/

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

As mentioned by @Lawrence Cherone, use clearInterval function:

new Vue({
  el: '#app',
  data: {       
    progressWidth: 0    
  },
  methods: {
    startProgress: function() {
      const that = this;
      const timer = setInterval( function() {
        if (that.progressWidth >= 100) {
          clearInterval(timer);
        } else {
          that.progressWidth += 10;
        }
      }, 150);
    }
  }
});
.progress{
    background-color: pink;
  transition: all 310ms ease;
    width: 0px;
    height: 20px;
  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  
  <div id="app">
    <button v-on:click="startProgress">Start Progress</button>
    <br><br>
    <div class="progress" :style="{width : progressWidth + 'px'}">{{ progressWidth }}</div>
  </div>

about 4 years ago · Santiago Trujillo 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