2011-06-22 34 views
0

上週我已經開始使用javascripts了,所以我的知識非常有限。請耐心等待我:-)

我正在寫一個簡單的預訂系統,在MySql中存儲日期和用戶ID。它檢查給定日是否已經預訂(如果是,則比isTicket.php返回UserId,如果它仍然是空閒的而不是返回0)。 由於可以選擇一個天數範圍,而且我不想發送多個警告,所以如果在任何選定日期其他用戶已被預訂,我將變量otherEventFound設置爲false。

如下圖所示,我試圖用這個變量「出邊」功能後,有兩種可能性:

1)如果我的腳本包含線>>警報(「otherEventFound .. 。< <它的工作原理。

2)如果我刪除此行,事實並非如此。

我有點迷失方向。有人可以解釋爲什麼這額外的警告線是非常重要的,一般多是否有可能從post.success函數覆蓋父函數中的變量?

如果純粹的運氣比警告線更有效,那麼在JavaScript中使用它的正確方法是什麼?

parent function 
... 
var otherEventFound = new Boolean(); 
var do_the_booking = new Boolean(); 
otherEventFound = false; 
do_the_booking = false; 
for (var i = myStart.getDfJ(); i <= myEnd.getDfJ(); i++) 
    { 
    // conver i to MySQL format yyyy-mm-dd 
    var milliseconds = 1000 * 60 * 60 * 24 *i; 
    var j = new Date(milliseconds); 
    var strID = j.toYMD(); 
    // and ask server if there is other event on this day 
    $.post("isTicket.php", { theDay: strID }, 
      function(answ){ 
       if (parseInt(answ) == 0){ 
        do_the_booking = true; 
       } 
       else { 
        if (!(parseInt(answ) == currentUserId)){ 
        otherEventFound = true; 
        } 
       } 
      } 
     ); 
    } 

    alert ("otherEventFound " + otherEventFound + " do_the_booking " + do_the_booking); 

    if (otherEventFound==true) { 
     alert ("There is not yours event booked on this day."); 
     do_the_booking=false; 
    }; 
    if (do_the_booking==true){ 
     var x=window.confirm("Do you want to book on this/these day/s?") 
     if (x) { 
      // ... do something like $.post("setTicket.php" ... 
     } 
    } 
+1

使用'新的布爾()'是一個壞主意,它的行爲以意想不到的方式,只是分配一個布爾值TRUE;或'假在'var'聲明中。使用['Boolean'對象](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Boolean)就像你不能在條件語句中可靠地使用它,'if(new Boolean(false )){}'將總是計算爲真,因爲對象被強制爲真。 JavaScript不使用嚴格的輸入,在存儲之前,您不必聲明要存儲哪種類型的變量值。 –

回答

3

當您執行$ .post時,會啓動一個異步AJAX請求,該請求會在稍後調用您的內聯函數。既然你不想要一段代碼執行,直到返回一個固定數量的異步請求,你將不得不跟蹤有多少請求已經完成。

它與警報「起作用」的唯一原因是警告插入了一個暫停,直到您回答了AJAX調用的完成點 並執行內聯函數。

你基本上要修改這樣的代碼:

var otherEventFound = false; 
var do_the_booking = false; 

var completeRequests = 0; 
for (var i = myStart.getDfJ(); i <= myEnd.getDfJ(); i++) 
{ 
    // do something 

    // and ask server if there is other event on this day 
    $.post("isTicket.php", { theDay: strID }, 
      function(answ){ 
       completeRequests++; 
       if (parseInt(answ) == 0){ 
        do_the_booking = true; 
       } 
       else { 
        if (!(parseInt(answ) == currentUserId)){ 
         otherEventFound = true; 
        } 
       } 

       if (completeRequests == myEnd.getDfJ()) { 
        postProcessing(); 
       }    
      } 
    ); 
} 

function postProcessing() { 
    alert ("otherEventFound " + otherEventFound + " do_the_booking " + do_the_booking); 

    if (otherEventFound==true) { 
     alert ("There is not yours event booked on this day."); 
     do_the_booking=false; 
    }; 
    if (do_the_booking==true){ 
     var x=window.confirm("Do you want to book on this/these day/s?") 
     if (x) { 
      // ... do something like $.post("setTicket.php" ... 
     } 
    } 
} 
+0

感謝您的回答和解釋。但是,此解決方案無法解決for循環中生成的多個警告問題。如果有人選擇f.e. 5天,其中3人已經預訂,比此代碼將3次開始提醒「沒有......」,並且兩次「你想......」。有沒有辦法來克服這種行爲? –

+0

我將整個預訂轉移到'setTicket.php'中。該腳本還處理多天以及其他用戶的現有工單。永遠不要多謝你的幫助。 –