Coder Social home page Coder Social logo

Multiple Timers about timer.jquery HOT 14 CLOSED

walmik avatar walmik commented on August 28, 2024
Multiple Timers

from timer.jquery.

Comments (14)

andysowards avatar andysowards commented on August 28, 2024

I've made some tweaks, if I put all of the functions inside of this block

// INITIALIZE THE PLUGIN
var pluginName = 'timer';
$.fn[pluginName] = function(options) {
options = options || 'start';

    return this.each(function() {

then it begins to work just fine on multiple instances, keeps track of the time, however, when I go to pause, it bugs out - it almost looks like it starts 2 timers on one - so its blinking between the old time and the new time in the same element, which i'm not sure what I should change now to fix that

Thanks!

from timer.jquery.

walmik avatar walmik commented on August 28, 2024

Thanks for your kind comments :)

The reason I did not add support for multiple timers is because of the fact that timers are a browser feature (not a JavaScript feature). In fact John Resig's article on the subject is what prompted me to think in this direction. If you re doing anything non trivial with timers, I d say, you take a look at it:

http://ejohn.org/blog/how-javascript-timers-work/

Having said that, I am open to ideas/suggestions/PRs that deal with this problem given the fact that we can now use Workers

from timer.jquery.

andysowards avatar andysowards commented on August 28, 2024

Awesome, I will check this out thanks for the link

I just need multiple timers to work as they do so that people can use them to time things and submit the time data via a form, which this will work perfect for if I can get it working correctly, i'll let you know if I figure anything out - do you know of any timers that allow multiple instances on a page?

Thanks for your time!

from timer.jquery.

shantanuthatte avatar shantanuthatte commented on August 28, 2024

How about if we use WebWorkers, if not then fallback to updating every timer element's data-seconds?

PS @walmik: This is too interesting plugin, I'm forking and will add ideas, you can accept PR later if you want..

from timer.jquery.

walmik avatar walmik commented on August 28, 2024

I ve been toying around with the idea of web workers for this but never got around to doing it due to time constraints. PRs (and ideas) are most welcome :)

from timer.jquery.

spedroq avatar spedroq commented on August 28, 2024

Hey guys, great plugin! I would like to let you know that I'm having issues with multiple timer instances, even though I give them unique ID's they still affect each other. (on a separate note: your seconds are not exactly accurate)

In any case let me know if there is a way to have multiple timers, or if it is a feature you guys havent implemented or are yet to fix.

Thanks for your time,
Pedro Q

from timer.jquery.

walmik avatar walmik commented on August 28, 2024

At the moment, it s a single timer implementation and we definitely want to support multiple timers. This task is open for current contributors or new. If no one picks it up, being the lead maintainer, I ll start this soon.

About the inaccurate seconds, please create a separate issue with steps to reproduce it or any info that can help in identifying the issue.

Above all, Thanks for your comments and interest :)

from timer.jquery.

mfsi-subhranil avatar mfsi-subhranil commented on August 28, 2024

Any update on multiple timers on same page? I need multiple timers in a page.

from timer.jquery.

walmik avatar walmik commented on August 28, 2024

Thanks for your concern and desire to use this plugin in your page. Unfortunately I do not have the bandwidth to implement it. Though I m open to PRs that will add this implementation.

from timer.jquery.

 avatar commented on August 28, 2024

Multiple timers would be amazing. I was using this plugin and got pretty far into development when I was asked to run timers concurrently, but unfortunately after reading this I find out it's not possible, which is too bad because the ease of use with this plugin is nice. Any recommendations for jquery timers that can run multiple or any advice on how to tweak the code to allow this happen or anything helpful? Not being able to do multiple timers is a huge shortcoming.

from timer.jquery.

walmik avatar walmik commented on August 28, 2024

I agree. It d be great to have multiple timers.

One way to implement (from a bird's eye view), currently the plugin treats all timer related variables singularly. It takes the current timestamp and every 500ms just calculates the offset from current timestamp. In other words, it does not increment a number by 1 in a setInteval (which is why it s so accurate). Hence, how bout maintaining the timer related variables in an associative array whose key will be each DOM element on which the timer is initialized?

Of course this is a very macro level thought/discussion. One other user suggested web workers. But I m personally tempted in this array idea as I don't have to worry about browsers that don't support web workers.

from timer.jquery.

rlmumford avatar rlmumford commented on August 28, 2024

I actually have this working with multiple timers. It wasn't too tricky.

I store the element on the Timer object, then log all the various counts against the element data instead of in variables in global scope.

This all works because you can bind a function to a given class, so I bind the element to the incrementSeconds method.

function startTimerInterval(element) {
    $(element).data('intr', setInterval(incrementSeconds.bind(element), options.updateFrequency));
    $(element).data('isTimerRunning', true);
}

See this commit for the working code. rlmumford@731da0c

from timer.jquery.

walmik avatar walmik commented on August 28, 2024

This looks great and thank you for putting thought and time to implement this much desired functionality! This is certainly the way forward. I m gonna play around with it a bit with multiple timers in a page. In the mean time, would you mind creating a PR for this change?

from timer.jquery.

walmik avatar walmik commented on August 28, 2024

Extremely happy to close this long standing issue! Thanks to the excellent work by @rlmumford !

from timer.jquery.

Related Issues (20)

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.