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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:11:57+00:00 2026-05-24T04:11:57+00:00

I have the following jQuery extension: var particleTimer = 0; jQuery.fn.particleEmitter = function (num,

  • 0

I have the following jQuery extension:

var particleTimer = 0;
jQuery.fn.particleEmitter = function (num, wid, hei, rad, tme, rep) {
   particleTimer = setInterval(function() {
      //appends elements...
   }, 500);
};

jQuery.stopParticleEmitter = function () {
  clearInterval(particleTimer); //this doesn't stop it...
}

But the clearInterval() doesn’t seem to work…

Here is a Fiddle of my complete code: http://jsfiddle.net/neuroflux/wtJFj/3/

Note that the “particles” won’t actually move as it requires the document width…

But the timer should clear onMouseout… and doesn’t…

  • 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-24T04:11:58+00:00Added an answer on May 24, 2026 at 4:11 am

    In terms of the actual setInterval, your code looks fine and I can’t see any reason that it wouldn’t work provided you only hever have one particle emitter running at any given time.

    I say that bit about “one at a time” because the first thing that jumped out at me is that you’re assuming no one using your plug-in will ever call your particleEmitter more than once, because you’re using a single variable for the handle.

    I’d make maintaining that handle the caller’s responsibility by returning it rather than keeping track of it yourself.

    jQuery.fn.particleEmitter = function (num, wid, hei, rad, tme, rep) {
       return setInterval(function() {
          //appends elements...
       }, 500);
    };
    
    jQuery.stopParticleEmitter = function(timerHandle) {
      if (timerHandle !== 0) {
          clearInterval(timerHandle);
      }
      return 0;
    }
    

    Returning the handle out of the particleEmitter means you can’t chain, but A) Your original didn’t support chaining (barring needing to return anything else, convention is to return this out of plug-in functions, for chaining); and B) One doesn’t always need to support chaining, not if you have a good reason for returning a different value.

    Side-note: I returned 0 out of the stopper function above because I tend to use this idiom in my code:

    myHandleVar = jQuery.stopParticleEmitter(myHandleVar);
    

    Obviously, that style may not be to your taste. 🙂

    Live working example of two emitters with the caller keeping track of the handles:

    HTML:

    <div id="container1">
      <input type='button' class='start' value='Start 1'>
      <input type='button' class='stop'  value='Stop 1'  disabled>
      <p class="target"></p>
    </div>
    <div id="container2">
      <input type='button' class='start' value='Start 2'>
      <input type='button' class='stop'  value='Stop 2'  disabled>
      <p class="target"></p>
    </div>
    

    JavaScript:

    jQuery(function($) {
      var handles = {};
    
      $("#container1, #container2").each(function() {
        var $this = $(this);
        $this.find(".start").click(startClick);
        $this.find(".stop").click(stopClick);
      });
    
      function startClick() {
        var container = $(this).closest('div[id^="container"]'),
            id = container[0] && container[0].id;
        if (id) {
          handles[id] = container.find(".target").particleEmitter();
          this.disabled = true;
          container.find(".stop")[0].disabled = false;
        }
          }
    
      function stopClick() {
        var container = $(this).closest('div[id^="container"]'),
            id = container[0] && container[0].id;
        if (id) {
          handles[id] = $.stopParticleEmitter(handles[id]);
          this.disabled = true;
          container.find(".start")[0].disabled = false;
        }
      }
    
      function display(msg) {
        $("<p>").html(msg).appendTo(document.body);
      }
    
    });
    

    Update: You asked below about stopping them all. That’s easily done, just keep track of the handles you hand out (live example):

    (function() {
      // Our current set of outstanding handles
      var handles = {};
    
      // Hook up our emitter function
      jQuery.fn.particleEmitter = function (num, wid, hei, rad, tme, rep) {
        var target = this,
            handle;
    
        // Start this interval timer
        handle = setInterval(function() {
          $("<span>click </span>").appendTo(target);
        }, 500);
    
        // Store the handle in a map
        handles[handle] = handle;
    
        // Return the handle
        return handle;
      };
    
      // Our "stop" function
      jQuery.stopParticleEmitter = function(timerHandle) {
        if (timerHandle !== 0) {
          // Clear the timer
          clearInterval(timerHandle);
    
          // Remove the handle from our "outstanding" list
          delete handles[timerHandle];
        }
    
        // Return a value the caller can use to clear our their
        // handle variable.
        return 0;
      }
    
      // Our "stop all" function
      jQuery.stopAllParticleEmitters = function() {
        var handle;
        for (handle in handles) {
          if (handles.hasOwnProperty(handle)) { // Largely unnecessary check
            clearInterval(handle);
          }
        }
        // Clear our outstanding handles. There's no race
        // condition here, JavaScript on browsers is single-threaded.
        handles = {};
      };
    })();
    

    Note how all of this is wrapped in a function, so the handles variable is private to our plug-in.

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

Sidebar

Related Questions

I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have the following jQuery code: $('.save').submit(function(e){ var formElement = $(this); var data =
I have the following jQuery extension: (function ($) { //jQuery plugin extension jQuery.fn.rotate =
I have the following jQuery: $(document).ready(function () { $(.element).draggable(); $(.element).droppable({ drop: function () {
I have the following jquery function for filtering the contents of a listbox on
I have the following jquery function > <script type=text/javascript> > > $(document).ready(function() { >
I have the following jQuery: $(form).submit(function() { //DO STUFF HERE }); But, it's not
I have the following jquery code: $(document).ready(function() { $(#build_table, a.coursename, .Start Date, .Book Title,
I have the following JQuery function being attached to the blur event of n
I have the following jquery: jQuery.fn.slideFadeToggle = function(speed, easing, callback) { return this.animate({ opacity:

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.