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

  • Home
  • SEARCH
  • 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 4051010
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:08:28+00:00 2026-05-20T14:08:28+00:00

this is my first atempt of creating a jquery plugin so I thought id

  • 0

this is my first atempt of creating a jquery plugin so I thought id start simple but even that is troubling…

heres my code.

(function($){
$.fn.elapsed = function(options) {
    var defaults = { seconds: true, minutes: true, hours: true, days: true };
    var options = $.extend(defaults, options);
    var ob = $(this);
    var secs = 0, mins = 0, hours = 0, days = 0;
    function elapsed_time( secs, mins, hours, days ){
        if( secs == 59 ) { mins++; secs = 0; }
        if( mins == 59 ) { hours++; mins = 0; }
        if( hours == 23 ) { days++; hours = 0; }
        ob.html( ( days === true ? days + ':' : false ) + ( hours === true ? hours + ':' : false ) + ( mins === true ? mins + ':' : false ) + ( secs === true ? secs + ':' : false ) );
        window.setTimeout( function(){ secs++; elapsed_time( secs, mins, hours, days ); }, 1000 );
    }
    elapsed_time( secs, mins, hours, days );    
};
})(jQuery);

nothing at all seems to happen… can any tell me where im going wrong?

  • 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-20T14:08:28+00:00Added an answer on May 20, 2026 at 2:08 pm

    You’re using the same variable names for your display which I think is unintentional, this:

    ob.html( ( days === true ? days + ':' : false ) + 
             ( hours === true ? hours + ':' : false ) + 
             ( mins === true ? mins + ':' : false ) + 
             ( secs === true ? secs + ':' : false ) );
    

    Should be more like this:

    ob.html( ( options.days ? days + ':' : '') + 
             ( options.hours ? hours + ':' : '') + 
             ( options.minutes ? mins + ':' : '' ) + 
             ( options.seconds ? secs : '') );
    

    In these conditionals you want to display the separator if showing that interval, not display false or 0 if not (I think this is your intent anyway). Since secs starts at 0 here, it was not changing at all, since 0 is falsey.


    Also, your intervals should changeover when they hit rather than just before, so for minutes/hours that’s 60 and days 24, like this:

    if( secs == 60 ) { mins++; secs = 0; }
    if( mins == 60 ) { hours++; mins = 0; }
    if( hours == 24 ) { days++; hours = 0; }
    

    ….and last this is already a jQuery object inside a plugin, so you can just do:

    var ob = this;
    

    overall, looking like this:

    (function($){
    $.fn.elapsed = function(options) {
        var defaults = { seconds: true, minutes: true, hours: true, days: true };
        var options = $.extend(defaults, options);
        var ob = this;
        var secs = 0, mins = 0, hours = 0, days = 0;
        function elapsed_time( secs, mins, hours, days ){
            if( secs == 60 ) { mins++; secs = 0; }
            if( mins == 60 ) { hours++; mins = 0; }
            if( hours == 24 ) { days++; hours = 0; }
            ob.html( ( options.days ? days + ':' : '') + 
                     ( options.hours ? hours + ':' : '') + 
                     ( options.minutes ? mins + ':' : '' ) + 
                     ( options.seconds ? secs : '') );
            window.setTimeout( function(){ secs++; elapsed_time( secs, mins, hours, days ); }, 1000 );
        }
        elapsed_time( secs, mins, hours, days );    
    };
    })(jQuery);
    

    You can test it out here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

problem euler #5 i found the solution but i don't know why this first
This is my first time attempting to call an ASP.NET page method from jQuery.
This is my first crack at a method that is run periodically during the
This is my first attempt at creating a package, so I must be missing
This is my first ever attempt at creating an svn repository and I'm confused.
This is my first attempt on semaphores and threads. I constructed this code from
This is my first-attempt at writing anything even slightly complicated in C++, I'm attempting
I'm using Visual Web Developer 2010 Express. This is my first attempt at creating
I was creating a simple program that simulates a coin toss for my class.
This is my first post here and I wanted to get some input 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.