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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:07:40+00:00 2026-06-09T19:07:40+00:00

I have created a jquery plugin with following structure: (function($){ var timer_handle, i =

  • 0

I have created a jquery plugin with following structure:

(function($){

var timer_handle, i = 5 ;

 $.my_plugin = function(){
  return{
    // provide $.my_plugin.reset() public method to reset
    reset : function(){ clearTimeout(timer_handle); }
  }
}();

 $.fn.my_plugin = function(){

    // init codes ...

    function tick(){
      i -= 1;
      console.log(i+'sec elapsed');
      if(i == 0){
      console.log('time over');   
      $.my_plugin.reset();
      }
        timer_handle = setTimeout(tick, 1000);
    }
    tick();
 }
 })(jQuery);

$('body').my_plugin();

When I see at console , after 0 sec elapsed, the counter is still running in negative,
ie the setTimeout has not been cleared.
As I examined , the public method
$.my_plugin.reset(); called from outside clear the timer,
but $.my_plugin.reset() called inside tick closure do not clear timer.

What may be the solution for such case ?????

  • 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-09T19:07:41+00:00Added an answer on June 9, 2026 at 7:07 pm

    Because you’re calling $.my_plugin.reset(); from inside the tick() function there is no timeout to clear. The previously scheduled timeout has already happened and that’s why tick() is executing. (This has nothing to do with whether the code is in a closure or not.)

    Instead, just don’t set the next timeout if i has reached 0:

    function tick(){
      i -= 1;
      console.log(i+'sec elapsed');
      if(i == 0){
        console.log('time over');
      } else {
        timer_handle = setTimeout(tick, 1000);
      }
    }
    

    If you were to call $.my_plugin.reset(); from some other code outside tick() then there would be an outstanding timeout that could be cleared so it would stop tick() running again.

    Note that declaring your timer_handle and i variables in that immediately invoked anonymous function means that all calls to your plugin will access those same variables so you will get incorrect behaviour if you run your plugin against more than one element on the page at the same time.

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

Sidebar

Related Questions

I created a jQuery plugin but I have a problem, I use the following
I have following jquery code plugin: (function ($) { $.fn.extend({ collapsiblePanel: function () {
I have created a jquery plugin to expand/collapse ul,li something like a accordion menu
I am using the jQuery Tools Scrollable plugin - http://flowplayer.org/tools/scrollable.html#navigator I have created a
Suppose I have created one jQuery function to check if elements exist or not
I have created a dialog with the JQuery dialog plugin. My dialog is defined
I am using the jquery-ui-map plugin (http://code.google.com/p/jquery-ui-map/). I have successfully created a map using
I have created a div using jquery and put some text in it: var
I have the following jQuery code: $('span.readMoreReputation').mouseover(function(event) { createTooltip(event); }).mouseout(function(){ // create a hidefunction
I have the following page: http://valogiannis.com/freelancer/neurosport/register.html I use a validation jquery plugin: http://docs.jquery.com/Plugins/Validation When

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.