0

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
6
  • I'm a bit confused as to what you're trying to achieve. With how you're currently doing it, it's impossible for it to run for 20 seconds.. because it'll loop through your array in like 1ms... Commented Jan 9, 2019 at 22:34
  • Do you want to run the script twice with 10 seconds in between? Commented Jan 9, 2019 at 22:37
  • Agreed, this does nothing right now. Are you trying to go from ["1"."2"."3"] to [["1",timestamp],["2",timestamp],["3",timestamp]] or perhaps [{"1",timestamp},{"2",timestamp},{"3",timestamp}] Commented Jan 9, 2019 at 22:53
  • @kemicofa I've changed my code to reflect the output. I've also edited my output. Commented Jan 9, 2019 at 23:05
  • The map function should only be used to produce a second array of transformed elements. Use forEach to iterate. Commented Jan 9, 2019 at 23:09

2 Answers 2

2

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);

}

Sign up to request clarification or add additional context in comments.

Comments

1

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;

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.