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 8019361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:22:03+00:00 2026-06-04T21:22:03+00:00

I’m new to Jquery plugin creation. Following jquery plugin is created using jQuery Boilerplate.

  • 0

I’m new to Jquery plugin creation. Following jquery plugin is created using jQuery Boilerplate. It just do a count-up and notify when count-up finished.
I want to have a function to restart count-up by setting counter to 0;
I dont understand how to call that reset function

  ;(function ( $, window, undefined ) {


        var pluginName = 'countup',
            document = window.document,
            defaults = {
                countSince: new Date(),
                countUpTo:120,
                notifyAfter:110,
                onExpire:function() {

                },          
            };

        // The actual plugin constructor
        function Plugin( element, options ) {
            this.element = element;
            this.options = $.extend( {counter:0}, defaults, options) ;
            this._defaults = defaults;
            this._name = pluginName;
            this.init();
        }

        Plugin.prototype.init = function () {
            this.tick();

        };
        Plugin.prototype.reset = function () {
            this.options.counter = 0;

        };
        Plugin.prototype.tick = function () {

                if (this.options.counter > this.options.countUpTo) {
                    //timer expired
                    this.options.onExpire($(this.element));
                }
                else {
                    if (this.options.counter > this.options.notifyAfter) {
                        $(this.element).find('span').html('<strong style="font-size: 15px; color:#ff0000;">' + this.options.counter+ ' seconds</strong>');
                    }
                    else {
                        $(this.element).find('span').html('<strong style="font-size: 15px; color:#3366ff">' + this.options.counter + ' seconds</strong>');
                    }

                    setTimeout(function() {
                        this.options.counter += 1;
                        this.tick();
                    }, 1000);//calling tick function again
                }       

        };  

        $.fn[pluginName] = function ( options ) {
            return this.each(function () {
                if (!$.data(this, 'plugin_' + pluginName)) {
                    $.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
                }
            });
        };

    }(jQuery, window));

on document ready ::

$('#countdown').countup({
                    onExpire:function() {
                        alert('hi');
                    },
                    countSince:new Date(),//count from this
                    countUpTo:30,//seconds from the countSince to expire
                    notifyAfter:20
})

after that i want to call reset() function on $(‘#countdown’). how to do that? Or is there a better way to write above code? Please give me some help here.

  • 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-06-04T21:22:07+00:00Added an answer on June 4, 2026 at 9:22 pm

    The boiler-plate code you created is awfully complicated. The following HTML and JavaScript (along with jQuery) will accomplish an upward counter from 0 until a number of seconds defined by var countTo at which point it will print the message stored in var successMsg. The reset button restarts the counter at zero.

    HTML:

    <div id="countdown"></div>
    <input id="countreset" value="Reset" type="button" />​
    

    JavaScript:

    var countTo = 5; // Time to count to
    var successMsg = 'hi'; // Replace count with this message at max number of seconds
    function countUp() {
        var countdown = $('#countdown');
        // Turn contents into integer
        var current = parseInt(countdown.html());
        // Increment seconds
        var next = current+1;
        if (next >= countTo) {
            // If at max seconds, replace with message
            countdown.html(successMsg);                
        } else {
            // Replace seconds with next second
            countdown.html(next);
            setTimeout(countUp, 1000);
        }
    }
    function startCountdown() {
        var countdown = $('#countdown');
        // Set to zero seconds
        countdown.html('0');
    
        // Start counting
        setTimeout(countUp, 1000);
    }
    $(document).ready(function() {
        // Start the countdown
        startCountdown();
        $('#countreset').click(function() {
            // On reset button .click(), start countdown
            startCountdown();
        });
    });​
    

    I’ve put the solution up on jsFiddle: http://jsfiddle.net/TxVnS/2/

    Update:
    A solution to allow for variable counter ids is here: http://jsfiddle.net/TxVnS/6/

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have thousands of HTML files to process using Groovy/Java and I need to

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.