Javascript根据逻辑不起作用

时间:2014-11-15 00:56:32

标签: javascript

我一直在努力寻找虫子但是找不到。我的javascript代码无法正常工作。

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);

});

当checkBookingStatus返回true并且cabBooked设置为1时,代码应该停止工作。但是根本没有停止。我不明白为什么。

1 个答案:

答案 0 :(得分:0)

if(!checkBookingStatus()) {如果错误则会继续,而不是根据您的评论是否属实。