How do I check a variable I receive latter different than the previous one

Discussion in 'JavaScript' started by ketting00, Nov 15, 2013.

  1. #1
    Is there anyway to check that a variable value I receive latter different than the previous one as below:
    
    function passData() {
       var i = Math.random();
       getData(i);
    }
    
    function getData(i) {
       this.i = i;
       console.log(i);
    }
    
    var button = document.getElementById('button');
    button.addEventListener('click', passData, false);
    
    Code (markup):
    In real life there is only two values of variable being received through websocket and I don't know how to separate them.
    Thank you,
     
    ketting00, Nov 15, 2013 IP
  2. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #2
    I did it. Put them in an array.
     
    ketting00, Nov 15, 2013 IP
  3. rehan.khalid.9235

    rehan.khalid.9235 Active Member

    Messages:
    78
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    85
    #3
    yes you can use array, but if you need to compare only with the last value, then you can use a variable.
    for example

    var lastvalue=0;
    function actionXYZ(value){
      // perform comparison of 'value' to 'lastvalue'
    
    //do action
    
    // in the end, update lastvalue
    lastvalue=value;
    }
    HTML:
     
    rehan.khalid.9235, Nov 30, 2013 IP