Soy nuevo en jquery, así que podría estar haciendo algunos. error obvio aquí, pero la instrucción if en la parte inferior no se está ejecutando.
Estoy haciendo un cronómetro y quiero que el cronómetro se detenga cuando llegue a 0, aunque tampoco sé cómo hacerlo.
var i = 60; $('button[id=skill]').click(function (e){ $(this).hide(1000) setInterval(function () { $("#stopWatch").html(i); i--; }, 1000); }); $("#resetButton").click(function (e) { i = 60;return false; }); if(i==0) { i=20 //I also want the stopwatch to stop when it reaches 0, don't know how to do that though. }Debe guardar el intervalo en una variable cuando lo cree. Luego puede borrar el temporizador cuando haya terminado.
var i = 60; var timer = null; $('#skill').click(function() { $(this).hide(1000); timer = setInterval(function() { $("#stopWatch").html(i); i--; if (i===0) { clearInterval(timer); } }, 1000); }); $("#resetButton").click(function() { i = 60; return false; }); EDITAR : he corregido un error lógico desde mi publicación original. La verificación de i==0 ahora está dentro del temporizador donde realmente puede ejecutarse. Simplemente lo copié del código original de OP sin mirar lo suficientemente cerca. ¡Arreglado ahora!