jquery - 4-States button (with .toggle()) and delayed Ajax in each state -
i've got 4-states button. know how delay execution of ajax request in order have 1 request @ end...
i mean, if press button twice, don't want execute first ajax request, second 1 after specific timeout.
$('.btn_mark_erreur').toggle( function (){ //state 1 var id_erreur = $(this).parent().attr('erreur_num'); $(this).attr('title','erreur réglée'); $(this).children('img').attr('src','img/erreur_ok.png'); settimeout(function(){ $.ajax({ type: 'post', url: "", datatype: ($.browser.msie) ? "text" : "xml", data: "a=maj_statut&data="+donnees , succes : function(data) { console.log(data);} }); },1000); }, function (){ //state 2 var id_erreur = $(this).parent().attr('erreur_num'); $(this).attr('title','erreur en cours'); $(this).children('img').attr('src','img/erreur_encours.png'); settimeout(function(){ $.ajax({ type: 'post', url: "", datatype: ($.browser.msie) ? "text" : "xml", data: "a=maj_statut&data="+donnees , succes : function(data) { console.log(data);} }); },1000); }, function (){ //state 3 var id_erreur = $(this).parent().attr('erreur_num'); $(this).attr('title','problème sur cette erreur'); $(this).children('img').attr('src','img/erreur_nok.png'); settimeout(function(){ $.ajax({ type: 'post', url: "", datatype: ($.browser.msie) ? "text" : "xml", data: "a=maj_statut&data="+donnees , succes : function(data) { console.log(data);} }); },1000); }, function (){ //state 0 var id_erreur = $(this).parent().attr('erreur_num'); $(this).attr('title','marquer comme...'); $(this).children('img').attr('src','img/erreur_statut.png'); settimeout(function(){ $.ajax({ type: 'post', url: "", datatype: ($.browser.msie) ? "text" : "xml", data: "a=maj_statut&data="+donnees , succes : function(data) { console.log(data);} }); },1000); } );
this code doesn't work, i've got request each state.
thanks help!
settimeout
returns value - identifier of timer. can pass value cleartimeout
have timer canceled. example:
var timers = new array(null, null, null, null); function cancelrequestsupto(n) { (int = 0; <= n; ++i) if (timers[i] != null) cleartimeout(timers[i]); } $("#btn_mark_error").toggle(function() { //... timers[0] = settimeout(function() { $.ajax(...); }, 1000); }, function() { timers[1] = ...; cancelrequestsupto(0); }, .... );
note once $.ajax
executes there no way (at least no way know of) can cancel request pending. suggest canceling timers set up.
Comments
Post a Comment