Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7404923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:19:35+00:00 2026-05-29T05:19:35+00:00

I’m working on my first jQuery plugin which is a simple countdown timer with

  • 0

I’m working on my first jQuery plugin which is a simple countdown timer with an option to set the target date. The goal is to get a plain text clock counting down to the provided target date & time. For the life of me I can’t figure out how to use setTimeout or setInverval within the plugin so it actually counts down. Spent all day digging through stackoverflow and other sources but couldn’t find a solution, so apologies if I’m just not getting it.

Here’s what I’ve got:

(function( $ ){

  $.fn.clock = function( options ) {  

    // ESTABLISH DEFAULTS
    var settings = $.extend( {
      'target' : '07/21/2013 09:00:00', // mm/dd/yyyy hh:mm:ss
    }, options);

    return this.each(function() {        

      // calculate milliseconds to target
      tarDate = new Date(settings.target);
      dateNow = new Date();
      amount  = tarDate.getTime() - dateNow.getTime();
      delete dateNow;

      // set label
      label = settings.title;

      // generate output
      if(amount <= 0) {
        out = '000:00:00:00';
      } else {
        td = 0; th = 0; tm = 0; ts = 0; out = ''; // reset everything
        amount = Math.floor(amount/1000);         // kill the milliseconds
        td     = Math.floor(amount/86400);        // calculate days to target
        amount = amount%86400;                    // convert amount to days
        th     = Math.floor(amount/3600);         // calculate hours to target
        amount = amount%3600;                     // convert amount to hours
        tm     = Math.floor(amount/60);           // calculate minutes to target
        amount = amount%60;                       // convert amount to minutes
        ts     = Math.floor(amount)+1;            // calculate seconds to target

    out += (td<=99?'0':'') + (td<=9?'0':'') + td + ':';
    out += (th<=9?'0':'') + th + ':';
    out += (tm<=9?'0':'') + tm + ':';
    out += (ts<=9?'0':'') + ts;
      }

      // assemble and pump out to dom
      $(this).html(out);

      // set refresh rate
      ??????

    });

  };
})( jQuery );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:19:35+00:00Added an answer on May 29, 2026 at 5:19 am

    Check out this link for an interesting pattern for plugin authoring. Basically what you need to do is provide a “method” for updating your clock:

    (function( $ ){
    
        function updateClock(element) {
            var settings = element.data("settings");
            // Your update logic goes here
        }
    
        $.fn.clock = function( options ) {  
            if ( options === 'update' )
                return updateClock(this);
            // Other methods, if any
            else if ( typeof options !== 'object' )
                throw 'Unknown method';
    
            // ESTABLISH DEFAULTS
            var settings = $.extend( {
                'target' : '07/21/2013 09:00:00', // mm/dd/yyyy hh:mm:ss
            }, options);
    
            // Save your settings for later access
            this.data("settings",settings);
    

    Then, every time you want to update an element, you call it this way:

    $(yourselector).clock("update");
    

    (from outside; if updateClock is accessible from your scope, you can call it directly for efficiency. Just remember to wrap the element in $() if necessary)

    Lastly, you have to configure setTimeout or setInterval. I would prefer setTimeout, because that will allow you to stop or restart your clock if necessary. Add this to the end of your updateClock, maybe preceeded by a check:

    setTimeout(function() { updateClock(element); }, settings.refreshRate);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.