0

I have an array (spliced to size 2) that keeps track of what user click (first, last). first and last elements are unique.

I am trying to load content based on what user clicked. The weird thing that is happening is that I don't see the updated array unless I do 2 console.logs. If 1 log is done the array doesn't get updated. I'm guessing this has something to do with array execution/manipulation time.

The way I debug is to a click handler to document and click to see the array value. Any suggestions or tips?

I never had this issue before. Thanks.

    var clicksInfo = [];

    $('#a, #b, #c').on('click', function(e){

    // array: add, splice
    if(jQuery.inArray($(this).attr('id'), clicksInfo) == -1){
        clicksInfo.push($(this).attr('id'));
        if(clicksInfo.length == 2){
          //  might do something
        }else if(clicksInfo.length == 3){
          clicksInfo.splice(0,1);
        }
      }else{
        clicksInfo.splice(0,1);
        clicksInfo.push($(this).attr('id'));      
      }

      if($(this).attr('id') == 'a'){
        //  do stuff.
      }else if($(this).attr('id') == 'b'){  
        //  do stuff.
      }else if($(this).attr('id') == 'c'){
        //  do stuff.
      }
    });


    $(document).on('click', function(){
      console.log('clicksInfo', clicksInfo);
      // console.log('clicksInfo', clicksInfo);
    });
2
  • 2
    Can we have a jsfiddle? It helps a lot to see the HTML :) Commented Oct 15, 2012 at 18:35
  • HTML part is not important. It's just 3 clickable divs. Here is the jsfiddle link. FYI, alert works as expected: jsfiddle.net/BH8Hm Commented Oct 15, 2012 at 18:43

2 Answers 2

2

Strings are strings, arrays are arrays, even in a console.log, so when doing :

console.log('clicksInfo', clicksInfo);

thats a string, a comma, and then an array ?

try doing:

console.log('clicksInfo : '+ clicksInfo);

to show the string representation, or to show the array as an object, don't mix it with other strangeness:

console.log(clicksInfo);
Sign up to request clarification or add additional context in comments.

3 Comments

This is the first time I'm seeing this, wow. The exact script work if you change console.log('clicksInfo', clicksInfo); to console.log(clicksInfo);.. It's like Chrome thinks it's the same thing if the first parameter is the same, and the 2nd is an array.
Yeah, this works. Never had an issue with logging arrays before. Thanks!
You can't just mix strings an arrays inside a console.log and expect the console to figure it out, most of the time you should pass objects alone to the console, and don't mix them with strings at all, just do another console log with the string alone etc. instead.
0

The problem you are facing is that events that occur are not guaranteed to execute in a specific order. Either click handler could be executed first, though in most browsers your handler for #a, #b, #c would be executed first.

While you could try to use setTimeout to wait long enough to synchronize your data, your code is quite likely to break.

If you want to handle a click in both cases, my recommendation would be to remove the handler for #a, #b, and #c. Use the document click handler only, pass in the event declaration, and then check the ID of the clicked element in your handler to invoke the first code. You are then guaranteed to have updated data before your second block of code runs. Something like this:

var clicksInfo = [];

$(document).on('click', function(e){

    if (e.target.id === "a" || e.target.id === "b" || e.target.id === "c") {
        // array: add, splice
        if (jQuery.inArray(e.target.id, clicksInfo) == -1){
            clicksInfo.push(e.target.id);
            if(clicksInfo.length == 2){
                 //  might do something
            } else if(clicksInfo.length == 3){
                clicksInfo.splice(0,1);
            }
        } else {
             clicksInfo.splice(0,1);
             clicksInfo.push(e.target.id);
        }

      if(e.target.id === 'a'){
        //  do stuff.
      }else if(e.target.id === 'b'){  
        //  do stuff.
      }else if(e.target.id === 'c'){
        //  do stuff.
      }
    }

    console.log('clicksInfo', clicksInfo);
});

JSFiddle here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.