javascript - ClearInterval() does not work on existing interval -
i have written following function school project, clearinterval()
not working. have looked @ several questions same problem , problem scope of time.
however, think function uses global variable, should work. doesn't, , have no idea why. please enlighten me.
html (actually part of table similiar rows)
<tr id="row35" data-uitval="1" class=""> <td> <button type="button" id="knopje" class="tabeluur" data-uur="35" onclick="check(this)"> <p>✕</p> </button> </td> <td> other cells... </td> </tr>
code (javascript, in <head>
-tag)
var fadingfunc; var busy = false; function check(uurid) { if(busy) { $('#foutmelding').html('uw vorige bewerking loopt nog. wacht totdat deze afgelopen.') } else { uitval(uurid); }; }; function uitval(uurid) { // makes button child (<p>) fade out , in continuously fadingfunc = window.setinterval(fadeblink(uurid), 2000); $.post( 'answerme.php', { // send stuff }, function(data) { if(data === 'succes: uitval toegevoegd') { window.clearinterval(fadingfunc); // stuff } else if(data === 'succes: uitval verwijderd') { window.clearinterval(fadingfunc); // stuff } else { window.clearinterval(fadingfunc); // stuff }; } ); };
note 1: stripped code something, since irrelevant question.
note 2: tested on computer (localhost, google chrome) , fadingfunc
hold value in global scope (tested via console in developers mode)
so if got - want x inside button blink while post request waiting response.
your code there not using setinterval
correctly , choosing poor approach achieve that. first fix code , check out approach using css animation.
to fix code:
- code must placed inside
<head>
tag since using inline call. - remove
window.
not needed - usesetinterval(
instead ofwindow.setinterval
. - when setting actuall interval function wrap target function function -
setinterval(function(){ fadeblink(uurid); }, 2000)
see work: jsfiddle
the second approach think better add class before request , remove when done. class obviuslly animated.
second approach: jsfiddle
second appraoch code:
js
var busy = false; function check(uurid) { if(busy) $('#foutmelding').html('uw vorige bewerking loopt nog.'); else uitval(uurid); } function uitval(uurid) { $(uurid).addclass('blink'); busy = true; $.post('http://fiddle.jshell.net/_display/', { test:'test' }, function() { $(uurid).removeclass('blink'); busy = false; } ); }
css
.blink p { -webkit-animation-name: blinker; -webkit-animation-duration: 1s; -webkit-animation-timing-function: linear; -webkit-animation-iteration-count: infinite; -moz-animation-name: blinker; -moz-animation-duration: 1s; -moz-animation-timing-function: linear; -moz-animation-iteration-count: infinite; animation-name: blinker; animation-duration: 1s; animation-timing-function: linear; animation-iteration-count: infinite; } @-moz-keyframes blinker { 0% { opacity: 1.0; } 50% { opacity: 0.0; } 100% { opacity: 1.0; } } @-webkit-keyframes blinker { 0% { opacity: 1.0; } 50% { opacity: 0.0; } 100% { opacity: 1.0; } } @keyframes blinker { 0% { opacity: 1.0; } 50% { opacity: 0.0; } 100% { opacity: 1.0; } }
Comments
Post a Comment