I have been struggling for last whole night to find the bug but could not find. My javascript code is not working as it should.
var currCab = 0;
var cabBooked = 0;
function sendMessage(cabId)
{
if(cabBooked == 1) {
return;
}
// get data from 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 from checkBookingStatus");
$("#simpleStatus").html("Cab booked. You can close tab now");
cabBooked = 1;
return true;
} else {
console.log("returning false from checkBookingStatus");
return false;
}
});
}
function tryCabAlloc()
{
if(!checkBookingStatus()) {
if(cabBooked == 1) {
return;
}
var checkid = document.getElementById("cabId"+currCab);
if(checkid==null) {
$("#simpleStatus").html("Cab could not be booked.");
$("#status").html("No more clients to communicate with.");
} else {
var cid = $("#cabId"+currCab).val();
sendMessage(cid);
updateTrying(cid);
currCab = currCab + 1;
}
}
}
$( document ).ready(function() {
$("#simpleStatus").html("Please wait. The communication is being started...");
$("#status").html("GCM push initiating ...");
tryCabAlloc();
setInterval(function(){
if(cabBooked==0) {
tryCabAlloc();
} else {
throw '';
}
},95000);
});
The code should stop working when the checkBookingStatus returns true and cabBooked is set to 1. But is not stopping at all. I have no idea why.