Skip to content

Instantly share code, notes, and snippets.

@notalentgeek
Last active January 10, 2017 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notalentgeek/327af98d6817ac7a7fd50e5675e86516 to your computer and use it in GitHub Desktop.
Save notalentgeek/327af98d6817ac7a7fd50e5675e86516 to your computer and use it in GitHub Desktop.
JavaScript function to detect interval (1, 2, 3, ... seconds) using operating system internal clock.
// Function to detect if x second is already passed.
// If each tick is more than 1 second then this
// function is screwed, but that will not happen
// (hopefully).
function DetectChangeInTSecond(_t, _date){
this.t = _t;
this.currSecond = _date.getSeconds();
this.currSecondPrev = this.currSecond;
this.counter = 0;
this.xSecondPassed = false;
}
DetectChangeInTSecond.prototype.Update = function(_date){
this.currSecond = _date.getSeconds();
if(this.currSecond != this.currSecondPrev){
this.counter ++
this.currSecondPrev = this.currSecond
}
else{ this.xSecondPassed = false; }
if(this.counter >= this.t){
this.counter = 0
this.xSecondPassed = true;
console.log(this.t + " second(s) has passed")
}
return this.xSecondPassed;
}
function Main(){
var currDateTime = new Date();
var dCITS1 = new DetectChangeInTSecond(1, currDateTime);
var dCITS3 = new DetectChangeInTSecond(3, currDateTime);
while(true){
currDateTime = new Date();
dCITS1.Update(currDateTime);
dCITS3.Update(currDateTime);
}
}
Main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment