Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.8k
Adding TimingFunctions/IntervalTimer#237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
itsvinayak commentedAug 1, 2020
@nandanvasudevan thanks for contributing, Please read Contribution Guidelines and run |
Sheraff commentedAug 6, 2020 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
This is all kinds of wrong. Your class isn't a singleton because a=newIntervalTimer()b=newIntervalTimer()console.log(a===b)// false A way to make a singleton that is close to what you wrote would be classIntervalTimer{constructor(){if(IntervalTimer.instance)returnIntervalTimer.instance// ... rest of constructor here}} Also, there is no reason to make it a singleton since JS supports very well having multiple
You don't actually measure time elapsed: startTimer(){this.timer=setInterval(this.callBack,this.interval)}/** *@return {number} Elapsed time since start. */getRunTime(){returnthis.timer} On a more conceptual level, The best pure-JS interval will always have to be blocking (in which case making it a singleton makes sense), but that's only useable in a threaded context (like a web worker or worker thread) because it blocks the execution of anything else. classBlockingInterval{constructor(callback=()=>{},interval=0){if(BlockingInterval.instance)returnBlockingInterval.instancethis.callback=callbackthis.interval=intervalthis.click(this.interval)}click(interval,start=performance.now()){while(performance.now()-start<interval){}this.callback()this.click(interval,start+interval)}}letlast=performance.now()consta=newBlockingInterval(()=>{constcurrent=performance.now()console.log(current-last)last=current},500) If you need an interval for drawing in the browser, use the same principle but with classFrameInterval{constructor(callback=()=>{},interval=0){this.callback=callbackthis.click(interval)}asyncclick(interval,start=performance.now()){while(performance.now()-start<interval){awaitnewPromise(requestAnimationFrame)}this.callback()this.click(interval,start+interval)}}letlast=performance.now()consta=newFrameInterval(()=>{constcurrent=performance.now()console.log(current-last)last=current},500) If you are more concerned w/ the average interval being correct, you should use a classDriftCompensatedInterval{constructor(callback=()=>{},interval=0){this.callback=callbackthis.interval=intervalthis.click()}click(drift=0){conststart=performance.now()setTimeout(()=>{this.callback()this.click((performance.now()-start)-this.interval)},this.interval-drift)}}letlast=performance.now()consta=newDriftCompensatedInterval(()=>{constcurrent=performance.now()console.log(current-last)last=current},500) |
nandanvasudevan commentedAug 8, 2020
I didn't know about any of these. Thanks for pointing it out. |
Welcome to JavaScript community
Describe your change:
Checklist:
Example:
UserProfile.jsis allowed butuserprofile.js,Userprofile.js,user-Profile.js,userProfile.jsare notFixes: #{$ISSUE_NO}.