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

时间:2019-01-09 22:25:35

标签: javascript arrays variables

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

2 个答案:

答案 0 :(得分:2)

我不明白您在这里要做什么,所以我的答案完全基于您的当前输出所需输出

您的timestamp变量是在global范围内声明的,因此,第一次调用myFunction时,其值为undefined,但在后续调用中它将保持一些值,从而产生“当前输出”。

要解决此问题,请将timestamp变量移到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);

}

答案 1 :(得分:1)

使用函数生成器和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;