Jack23
Jack23

Reputation: 1396

How to use a variable

I have a question / problem about a variable.

I have two page, in the first one I recover data and in the second one I do some operations.

ActivityPage.js (the first one)

recoverActivity() {
  // this function check every second if the size of array > 1000
  // this call only a function in the other page (Operations)
  Operations.write({
    arrayTimestamp: this.arrayTimestamp,
    // other things
  });
}

//this function when the user click a stop button.
stopActivity() {
  Actions.Operations({
    arrayTimestamp: this.arrayTimestamp,
  });
}

And the I have another page

Operations.js:

//this is called from the first page directly
write(objectData) {
  //...
  this.timestampCheck(objectData.arrayTimestamp);
  //...
}

//this is call from the ComponentDidMount of the second page.
stopClick() {
  //...
  this.timestampCheck(this.props.arrayTimestamp);
  //...
}

Now my problem is in this timestampCheck function:

timestampCheck(timestamp) {
  var int_max = 65536;
  this.base = 0;
  var diff = "";
  var start = parseInt(this.contatore);
  for (let i = 0; i < timestamp.length; i++) {
    let timestamp = parseInt(timestamp[i]);
    diff = (this.base + timestamp) - start;
    if (diffDestro < 0) {
      this.base+= int_max;
      diff += this.base;
    }
    this.tempoReale.push(diff);
  }
}

This function is called from the two function stopClick and write and there I have a variable this.base. Now I don't want that this variable loose his value when it leaves the functions timestampCheck. For example the arrayTimestamp has a size > 1000 an so it call the write() functions. here calculations are made and the value of this.base is set.

At this point, if the user clicks the stop key, the stopClick () function is called which calls the same timestampCheck function and must resume the previous value of this.base and not start from scratch.

How do you think I can do it?

thank you so much.

Upvotes: 1

Views: 77

Answers (1)

RStevoUK
RStevoUK

Reputation: 456

Just use a variable outside of the function to store the new value.

So outside of the function:

    var countingValue = 0;

    function timestampCheck(timestamp) {
 
        var int_max = 65536;

        this.base = 0;

        var valueToUse = countingValue > 0 ? countingValue : this.base;

        var diff = 0;

        var start = parseInt(this.contatore);

        for (let i = 0; i < timestamp.length; i++) {

          let timestamp = parseInt(timestamp[i]);

          diff = (valueToUse + timestamp) - start;

          if (diffDestro < 0) {
            valueToUse += int_max;
            diff += valueToUse;
          }

          this.tempoReale.push(diff);

          countingValue = countingValue + diff;
        }
    }

So what I have done here is create a variable outside of the function named countingValue with an initial value of 0.

Then underneath the initialisation of this.base I have used a type of If statement known as a ternary operator which says if the current countingValue is more than 0 then we will store that value in a variable named valueToUse otherwise we will use the this.base value and store it in the valueToUse variable.

In the rest of the code I have used the valueToUse variable for the computations now instead of this.base.

Note: I changed your variable diff to an integer because it was a string. You may want to review this and swap a couple of variables around if it's not exactly what you want.

Upvotes: 1

Related Questions