How do i keep changing unique time like 1445787528572 every 5 sec on the client side


#1

hi
I have a requirement where I need to generate a unique time stamp every 5 second using the format 1445787528572 time format. unique.datetime format.

I want to change the timestamp automatically every 5 sec on the client side. Pls help


#2

SetInterval documentation


#3

i have tried this but no luck

in my client js file template helper

 function uniquetimestp() {
        var uniqtime = new Date().getTime();
        return uniqtime;
    };
Template.singleCamView.helpers({

 timestp: function(){
            console.log('timestamp: ');
            Meteor.setInterval(uniquetimestp, 5000);
            return uniquetimestp();
        }

})

html code:

 <div id="camViewDiv1">
                    <img name='webcam' id="videoImg1" src="{{snapshoturl}}+{{timestp}}" style="width: 99.9%; height: 99.9%; float: left; padding-top: 10px"/>
                    <iframe name="main" height="1" width="1"></iframe>
                </div>

#4

in Template.singleCamView.onCreated define template level ReactiveVar
and in same onCreated define that setInterval to change value of that reactive variable, for example uniqueTimestamp

than in helper just return
Template.instance().uniqueTimestamp.get()

Problem with your example is that you are not using any reactive source, so the template is not rerun
And also you dont want to run setInterval multiple times, as it is already executing function 10000000+ times till you stop it.

PS: I think it would be also good to destroy that interval in onDestroyed, so it does not keep running after you navigate elsewhere


#5

you mean like this

   function uniquetimestp() {
        this.uniqueTimeStamp = new Date().getTime();
        return this.uniqueTimeStamp;
    };

    Template.singleCamView.onCreated(function(){

        this.uniqueTimeStamp = new ReactiveVar();
        Meteor.setInterval(uniquetimestp, 5000);

    })



    Template.singleCamView.helpers({

        timestp: function(){
            console.log('timestamp: ' + uniquetimestp());
            return uniquetimestp();
        }


    })

#6
Template.singleCamView.onCreated(function(){
  self = this;
  this.uniqueTimeStamp = new ReactiveVar();
  this.ourInterval = Meteor.setInterval(function(){
    self.uniqueTimeStamp.set(new Date().getTime());
  }, 5000);
})



Template.singleCamView.helpers({
  timestp: function(){
    newStamp = Template.instance().uniqueTimeStamp.get();
    console.log('timestamp: ' + newStamp);
    return newStamp;
  }
})

Template.singleCamView.onDestroyed(function(){
  Meteor.clearInterval(this.ourInterval);
})

PS: I am normally using coffeescript, so add these var, ; and other stuff yourself if it is missing


#7

perfect it worked thanks a ton!!