Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
itsvinayak merged 3 commits intoTheAlgorithms:masterfromnandanvasudevan:master
Aug 1, 2020

Conversation

@nandanvasudevan
Copy link
Contributor

Welcome to JavaScript community

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have readCONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
    Example:UserProfile.js is allowed butuserprofile.js,Userprofile.js,user-Profile.js,userProfile.js are not
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message containsFixes: #{$ISSUE_NO}.

itsvinayak reacted with thumbs up emojiSheraff reacted with thumbs down emoji
@itsvinayakitsvinayak added the code style issueFailing style checks labelAug 1, 2020
@itsvinayak
Copy link
Member

@nandanvasudevan thanks for contributing,

Please read Contribution Guidelines and runstandard --fix command
Thank you
@itsvinayak

nandanvasudevan reacted with thumbs up emoji

@itsvinayakitsvinayak merged commit1841d1f intoTheAlgorithms:masterAug 1, 2020
@Sheraff
Copy link

Sheraff commentedAug 6, 2020
edited
Loading

This is all kinds of wrong.

Your class isn't a singleton becausethis.instance always refers tothis and not the class itself.

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 multiplesetInterval running concurrently, so your description is wrong:

The class is singleton as javascript does not support multiple timer instances

You don't actually measure time elapsed:setInterval returns an ID meant to be passed toclearInterval and isn't specifically related to any time measurements. This logic is wrong ingetRunTime as well as ingetElapsedTime:

startTimer(){this.timer=setInterval(this.callBack,this.interval)}/**  *@return {number} Elapsed time since start.  */getRunTime(){returnthis.timer}

On a more conceptual level,setInterval isn't particularly well suited to keep track of time because it can be delayed arbitrarily. One should prefer thePerformance API if possible (ES, but not Node). If your context doesn't have theperformance object, you can replaceperformance.now() withDate.now() in each of the following examples (which will result in a slightly less accurate interval).

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 withrequestAnimationFrame. It will be less precise but as close as can be to a browser frame:

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 asetTimeout and adjust the delay on each iteration:

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)
ms10398 and nandanvasudevan reacted with thumbs up emoji

@nandanvasudevan
Copy link
ContributorAuthor

I didn't know about any of these. Thanks for pointing it out.
I will start working on the fix.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@itsvinayakitsvinayakitsvinayak approved these changes

Assignees

No one assigned

Labels

code style issueFailing style checks

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@nandanvasudevan@itsvinayak@Sheraff

[8]ページ先頭

©2009-2025 Movatter.jp