Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
¿Cómo puedo detener Interval en VueJS?

Intento hacer una barra de progreso con VueJS, pero cuando este recuento es igual a 100, el intervalo sigue en proceso. ¿Cómo puedo detener el intervalo cuando los datos equivalen al 100 por ciento?

Usé el comando console.log() para verificar si el código funciona, pero devuelve una respuesta incorrecta.

Saludos,

Ese es mi código:

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('--'); } }, });

si lo desea, puede verificar en vivo en jsFidde: https://jsfiddle.net/6j05p8Lb/46/

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Como lo mencionó @Lawrence Cherone , use la función clearInterval :

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!