TrimTrimm
TrimTrimm

Reputation: 113

How can I set unique variable foreach item in array with javascript?

I want to set a time stamp for each number, and then test each numbers timestamp against its self.

        var timestamp;
        nums=["1", "2", "3"];
        nums2=nums.map(myFunction);


        function myFunction(num) {

          setInterval(function() {

            var current_time = Math.floor(Date.now() / 1000);

            if (typeof timestamp !== "undefined" ) {
                if (current_time > (timestamp + 60)) {
                    timestamp = Math.floor(Date.now() / 1000);
                    console.log('time stamp reset');
                } else {
                    console.log('time stamp too young will reset after 60 sec');
                }
            } else {
                timestamp = Math.floor(Date.now() / 1000);
                console.log('time stamp set');
            }

         }, 10000);

        }

****If I run script for 20 sec:****

current output:

time stamp set

time stamp too young will reset after 60 sec

time stamp too young will reset after 60 sec

*(10 seconds later)*

time stamp too young will reset after 60 sec

time stamp too young will reset after 60 sec

time stamp too young will reset after 60 sec

desired output:

time stamp set

time stamp set

time stamp set

*(10 seconds later)*

time stamp too young will reset after 60 sec

time stamp too young will reset after 60 sec

time stamp too young will reset after 60 sec

Upvotes: 0

Views: 225

Answers (2)

kockburn
kockburn

Reputation: 17616

Using function generators and setInterval

const data = [];
//populate data
for(let i = 0; i < 3; i++){data.push(i)}

function * gen(){
  const timestamp = Date.now();
  let currentTimestamp;
  do{
     currentTimestamp = yield Date.now();
  }while(currentTimestamp - timestamp < 60000);
  return;
}

function run(){
  let iter = gen();
  let t = null;
  let i = 0;
  const res = [];
  const si = setInterval(function(){
    const {done,value} = iter.next(t);
    if(done){
      console.log("60 seconds have gone... reseting...")
      iter = gen();
      //reseting
      i = res.length = 0;
      
    }
    t = value;
    if(i >= data.length){
      console.log("waiting...");
      return;
    }
    console.log("Set timestamp");
    const d = [data[i], value];
    console.log(d);
    res[i++] = d;
  }, 1000);
}

window.onload = run;

Upvotes: 1

Mayank K Rastogi
Mayank K Rastogi

Reputation: 604

I do not understand what you are trying to do here, so my answer is purely based on your current output and desired output.

Your timestamp variable is declared in the global scope, so the first time myFunction is called, it's value is undefined, but on subsequent calls it will be holding some value, resulting in your "current output".

To fix it, move timestamp variable inside myFunction.

nums=["1", "2", "3"];
nums2=nums.map(myFunction);


function myFunction(num) {
  var timestamp;

  setInterval(function() {

    var current_time = Math.floor(Date.now() / 1000);

    if (typeof timestamp !== "undefined" ) {
        if (current_time > (timestamp + 60)) {
            timestamp = Math.floor(Date.now() / 1000);
            console.log('time stamp reset');
        } else {
            console.log('time stamp too young will reset after 60 sec');
        }
    } else {
        timestamp = Math.floor(Date.now() / 1000);
        console.log('time stamp set');
    }

 }, 10000);

}

Upvotes: 2

Related Questions