GithubHelp home page GithubHelp logo

llucax / pausable_timer Goto Github PK

View Code? Open in Web Editor NEW
27.0 27.0 6.0 162 KB

A Dart timer that can be paused, resumed and reset.

Home Page: https://pub.dev/packages/pausable_timer

License: BSD 3-Clause "New" or "Revised" License

Shell 12.48% Dart 87.52%
dart dartlang timer

pausable_timer's Introduction

Me

Hi! I'm a software developer that loves Free/Open Source Software and is trying to make a living out of it. I've been involved in FOSS for more than 20 years now. I've founded local Linux users groups and contributed to FOSS projects and wrote a few myself. Please check my GitHub Sponsors page for more details about me.

If you want to check my (past) contributions, I also have (had) other GitHub work account from which I also contribute(d) to FLOSS projects:

If you want to support me, I can accept sponsoring via these platforms (from the most preferred to the less preferred):

GitHub Sponsors Liberapay Paypal Buy Me A Coffee Patreon Flattr

Check the summary on sponsoring platforms to find out the differences between them (fees, features, etc.).

pausable_timer's People

Contributors

github-actions[bot] avatar llucax avatar mateusfccp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pausable_timer's Issues

Add a periodic timer flavor

There has been already a couple of requests for an easy way to create a pausable periodic timer in #19.

People seems to expect a PausableTimer.periodic() constructor, even when it's trivial to build on top of PausableTimer bacause it trivial to reset it and start it again.

Example:

timer = PausableTimer(
    Duration(seconds: 1),
    () {
      // This is really what your callback do.
      print('A period has passed');

      // we know the callback won't be called before the constructor ends, so
      // it is safe to use !
      timer
        ..reset()
        ..start();
    },
  )..start();

Once you want your timer to go away, you can just .pause() it and that's it. There is the caveat that if the actual work your callback does takes too long, the period will stretch. If you reset the timer before doing the actual work, it can be called without any further wait if it took longer than the period. Finally the time it takes for the work to be done can be calculated and the duration of the timer adjusted accordingly, which gets much less trivial, so maybe it is a worthy addition anyway ๐Ÿค”

Trying to make a countdown timer for video recording, but the callback is fired only once.

I tried the same with the inbuilt Timer.Periodic(), but pausing and resuming was so erratic; I had a time limit of 10 seconds, but pausing and resuming multiple times causes the total recording time to go over that limit.
I stumbled across this library from an SO answer.

When I replaced Timer.Periodic with PausableTimer, I see that it is fired only once.

Here is the sample code:

 int _start= 10;

 void startTimer() {    
    _timer = PausableTimer(
      Duration(seconds: 1),
      () {
        if (_start <= 0) {         
          recordVideo();
          setState(() {
            _isRecording = true;
          });
        } else {
          setState(() {
            _start = _start - 1;            
          });          
        }
      },
    )..start();
  }

This method will be called when the recording button is pressed.
recordVideo() is what I call to either start recording (along with startTime()) or end recording.

I know you're busy and haven't touched Dart in years, but can you help me understand what is that I'm doing wrong here and why is it being called only once?

Pause timer periodic?

EDIT: See #45 for an example on how to implement this.

Declaring and using the timer in init state does not work like timer.periodic.

What is meaning by "pause"?

I try to have a simple timer and need to pause it:

late final PausableTimer timer;

timer = PausableTimer(Duration(seconds: 1), () {
    print('Fired!');
    timer
        ..reset()
       ..start();
    });
    timer.start();

now I want to pause it:

timer!.pause();

But after starting again with timer!.start(); it starts from millisecond 0.
This is not good because I never hit exactly at millisecond 0 when pausing, but maybe at 323 millisecond or similar.

How can I restart the timer from the exact position where i paused it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.