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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:53:04+00:00 2026-06-04T14:53:04+00:00

I was playing with a fiddle earlier today while trying to answer a question

  • 0

I was playing with a fiddle earlier today while trying to answer a question and found a confusing thing. Being a JS newbie I am not being able to debug whats going wrong myself. I even tried to check the source0 of $.fn.show in jQuery source but couldn’t figure out whats going wrong.

HTML:

<input type='text' id='dataBox'/>
<input type='button' value='toggle' id='toggleButton' />​

jQuery code:

jQuery(function ($) {
    var _oldShow = $.fn.show;
    $.fn.show = function (speed, oldCallback) {
        return $(this).each(function () {
            var obj = $(this),
                newCallback = function () {

                    if ($.isFunction(oldCallback)) {
                        oldCallback.apply(obj);
                    }
                    obj.trigger('afterShow');
                };
            obj.trigger('beforeShow');
            if(speed)    
                _oldShow.apply(obj, [speed,newCallback]);
            else    
                _oldShow.apply(obj, [newCallback]);
        });
    }
});


$('#dataBox').bind('beforeShow', function () {
        alert('beforeShow');
    });


$('#toggleButton').click(function(){
        $('#dataBox').show();
}); 

The problem is for some mistake that I did, is causing this line to execute infinite number of times
obj.trigger(‘beforeShow’);

and hence the alert in this block

 $('#dataBox').bind('beforeShow', function () {
    alert('beforeShow');
 });  

seems not to stop.

Irrespect of what I am trying to do or if this can be done any other way, can someone please explain what I am doing wrong here. I have been trying for several hours but couldn’t figure out.

FIDDLE

  • 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-04T14:53:06+00:00Added an answer on June 4, 2026 at 2:53 pm

    The problem with the above code is that at some point jQuery calls the $.fn.show from within itself and that creates the infinite loop. So the proper way to prevent that is to do some argument checking like follows:

     jQuery(function($) {
        var _oldShow = $.fn.show;
        $.fn.show = function(speed, easing, oldCallback) {
    
            var args = Array.prototype.slice.call(arguments),
                duration = args[0] || 0,
                easing = 'linear',
                callback = function() {},
                callbackArgIndex = 1;
    
            // jQuery recursively calls show sometimes; we shouldn't
            //  handle such situations. Pass it to original show method.    
            if (!this.selector) {
                _oldShow.apply(this, args);
                return this;
            }
    
            if (args.length === 2) {
                if ($.isFunction(args[1])) {
                    callback = args[1];
                    callbackArgIndex = 1;
                }
                else {
                    easing = args[1];
                }
            }
            else if (args.length === 3) {
                easing = args[1];
                callback = args[2];
                callbackArgIndex = 2;
            }
    
            return this.each(function() {
                var obj = $(this),
                    oldCallback = callback,
                    newCallback = function() {
    
                        if ($.isFunction(oldCallback)) {
                            oldCallback.apply(obj);
                        }
                        obj.trigger('afterShow');
                    };
                obj.trigger('beforeShow');
                args[0] = duration;
    
                if (callback) {
                    args[callbackArgIndex] = newCallback;
                }
                else {
                    args.push(callback);
                }
    
                _oldShow.apply(obj, args);
    
    
            });
        }
    });
    
    
    $('#dataBox').bind('beforeShow afterShow', function(e) {
        alert(e.type);
    });
    
    
    $('#toggleButton').click(function() {
    
        $('#dataBox').show();
    
    });​
    

    This works fine and the events are fired properly.

    The block which is preventing the infinite loop is this

    if (!this.selector) {
          _oldShow.apply(this, args);
          return this;
    }
    

    What this is doing is, calling the original function and returning in cases jQuery calls $.fn.show multiple times(jQuery seems to be doing so for some reason).

    Working Fiddle

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

Sidebar

Related Questions

While playing around with the emulator, I noticed that when trying to view a
Playing with jquery for the first time, and I'm trying to get a simple
Playing around with MongoDB and NoRM in .NET. Thing that confused me - there
This is going to seem a very strange question but im just playing about
While playing around with playframework yabe-siena-gae, I noticed a datastore file created for the
I am playing with the wcf 4.0 rest template and trying to get it
Playing with Git and GitHub,I found that sometimes a git commit -a is needed
Im playing around with Recurly, the app enables real time push notifications. My question
Recently playing around with the open source iphone app code, and found it uses
Just playing around with the now released Silverlight 2.0. I'm trying to put a

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.