Javascript not working as per logic -
this question has answer here:
- how return response asynchronous call? 24 answers
i have been struggling last whole night find bug not find. javascript code not working should.
var currcab = 0; var cabbooked = 0; function sendmessage(cabid) { if(cabbooked == 1) { return; } // data html page $.post("sendmessage.php",data, function(data,status){ $("#status").html(data); }); } function checkbookingstatus() { var data = "bookingid="+$("#bookingid").val(); $.post("checkbookingstatus.php",data, function(data,status){ // callback here var ans = data.indexof("true"); if(ans!= -1) { console.log("returning true checkbookingstatus"); $("#simplestatus").html("cab booked. can close tab now"); cabbooked = 1; return true; } else { console.log("returning false checkbookingstatus"); return false; } }); } function trycaballoc() { if(!checkbookingstatus()) { if(cabbooked == 1) { return; } var checkid = document.getelementbyid("cabid"+currcab); if(checkid==null) { $("#simplestatus").html("cab not booked."); $("#status").html("no more clients communicate with."); } else { var cid = $("#cabid"+currcab).val(); sendmessage(cid); updatetrying(cid); currcab = currcab + 1; } } } $( document ).ready(function() { $("#simplestatus").html("please wait. communication being started..."); $("#status").html("gcm push initiating ..."); trycaballoc(); setinterval(function(){ if(cabbooked==0) { trycaballoc(); } else { throw ''; } },95000); });
the code should stop working when checkbookingstatus returns true , cabbooked set 1. not stopping @ all. have no idea why.
if(!checkbookingstatus()) {
continue if it's false, not if it's true per comment.
Comments
Post a Comment