2011-07-17 1 views
2

申し訳ありませんが、私は今一日以上頭を傷つけています。そして、私はそれが解決するのがとても簡単だと確信しています。JSは特定の行の後でコードの処理を停止するのはなぜですか?

以下の関数は、phpファイルからいくつかの投稿データを取得します(xml形式が返されます)。成功すると、カート内の項目がこの項目に既に存在するかどうかをチェックして同じ名前で2回追加されています。したがって、アイテムの現在のリストをループし、一致するものを見つけます。それが見つからない場合は、項目を上手く追加します。存在する場合は、その行を新しい金額で更新する操作が必要です。

いくつかの奇妙な理由により、一致があるとすぐにコードを実行することができません。 if(match == true)は、console.log("index found present status: "+match);がコンソールのtrueを返すにもかかわらず、何もしません。

これはどのように可能ですか?スコープなどと関係がありますか?

は、そこにある関数の2番目の呼び出しに、重複して1つのアイテムにマージする必要があることを確認するためだけにあります。毎回ウェブサイト上のものをクリックして保存します。

function addToCart (ueid, amount) 
    { 
    // you can remove the follow alert() call, typically this function would 
    // scroll (through an #anchor or maybe tween/scroll with jQuery) down to 
    // the area below the panorama tour and there display the user-interface 
    // for buying/sponsoring items 

    console.log("buy "+ueid+" "+amount+" times"); 
    var match = false; 

    console.log("before ajax present status: "+match); //output: false 

    //Get detailed info about this item 
    iteminfo = jQuery.ajax(iteminfourl, { data: { "ueid": ueid }, type: "POST", 
    success: 
     function(data) 
     { 
      console.log("ajax success present status: "+match); //output: false 
      totalprice = (amount * $(data).find('price').text()); 

      //first check if the item already exists in the table 
      currentContents = basket.fnGetData(); 

      if(currentContents.length > 0) 
      { 
       for(index = 0; index <= currentContents.length; index++) 
       { 
        if(currentContents[index][0] == ueid) 
        { 
         console.log("ueid and index are the same"); 

         match = true; 
         var updateIndex = index;      
         console.log("index found present status: "+match); //output: true 
        } 
       } 
      } 

      if(match == true) 
      { 
       //this never gets executed, even if match is set to true. 
           //also console.logs just before the if statement are only 
           //executed when match = false all the way... 
           console.log("item is present, update index "+updateIndex); 
       //basket.fnUpdate() 
      } 
      else 
      {   
       basket.fnAddData(
       [ 
        ueid, 
        amount, 
        totalprice, 
        'X' 
       ] 
       ); 

       if(basket.fnGetData().length == 1) 
        addToCart('window', 1); 
      } 
     } 
    }); 
} 
+1

なぜiteminfo = jQuery.ajax()?? – DrStrangeLove

+0

は予約済みの世界ですので、* does_it_match *のようなsthgに変更して再試行することができます。 – hornetbzz

答えて

3
for(index = 0; index <= currentContents.length; index++) 

for(var index = 0; index < currentContents.length; index++) 

キービーイング使用<代わりのループ発現の<=であるべきです。 currentContents[1 too many][0]を実行すると、あなたのループが余分な時間を稼ぎ、TypeErrorを投げていると思います。エラー警告が表示されるようにデバッグコンソールを見てください。

+0

実際には 'currentContents [index]'は 'index'が範囲外になると未定義になります。したがって、未定義の最初の項目にアクセスしようとすると厄介なエラーがスローされます。正しい答えは+1です。 :) –

+0

実際に問題を修正しましたが、奇妙なことはコンソールにエラーを出力しないということです。他の 'console.log()'を出力するだけで停止します。なぜそれが起こるかもしれないのか? FirefoxとFirebugの両方でテストしていますが、どちらもエラーは表示されません。 – Coen

+0

私はそれについては分かりません。このjsfiddleを実行すると、Chromeは「Uncaught TypeError:クロムインスペクタパネルで未定義のプロパティ '0'を読み取れません」というメッセージを表示します。 http://jsfiddle.net/8PJAy/ –

関連する問題