Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does JS stop processing my code after a certain line?
    primarykey
    data
    text
    <p>Alright, I have been breaking my head over this one for more than a day now. And I'm sure it's very simple to solve.</p> <p>The function below is supposed to do this: get some post data from a php file (xml format returned), on success process that data to check if an item in the cart already exists with this item, to prevent it from being added twice under the same name. So it loops through the current list of items and finds a match. If it's not found, it will add the item just fine. If it is there, a manipulation needs to take place to update that row with the new amount.</p> <p>For some strange reason I can't get the code to execute as soon as there is a match. <code>if(match == true)</code> just doesn't do anything, even though <code>console.log("index found present status: "+match);</code> returns <code>true</code> in the console just fine a few lines above that.</p> <p>So how is this possible? Does it have to do with scope or something?</p> <p>The <code>if(basket.fnGetData().length == 1) addToCart('window', 1);</code> is only there to make sure the second call of the function there is a duplicate item that needs to be 'merged' into one. Saves me clicking on stuff on the website every time.</p> <pre><code> 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 &gt; 0) { for(index = 0; index &lt;= 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); } } }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload