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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:16:42+00:00 2026-06-07T03:16:42+00:00

I have a situation where I need to use jQuery’s $.fn.one() function for a

  • 0

I have a situation where I need to use jQuery’s $.fn.one() function for a click event, but I don’t want it to apply to the next occurrence of the event (like it usually does), I want it to apply to the occurrence immediately after that, and then unbind itself (like .one() normally does).

The reason I don’t want .one() to apply to the first occurrence is because I’m binding to the document from an event handler invoked earlier in the bubbling phase, so the first time it gets to document it’ll be part of the same event. I want to know when the very next click event occurs.

Note: I do not want to use .stopPropagation() because it will potentially break other parts of my app.

Here are the two options I’ve come up with, though it seems like there must be a more elegant solution.

The double bind method:

$(document).one('click', function() {
  $(document).one('click', callback);
});

The setTimeout method:

setTimeout(function() {
  $(document).one('click', callback);
}, 1);

Both methods work just fine, but here’s my question. I have no idea what the performance implications are for either setTimeout or frequent event binding and unbinding. If anyone knows, I’d love to hear it. But more importantly, I’d like some suggestions on how to measure this stuff myself for future situations like this.

I love sites like http://jsperf.com, but I don’t know if it would really be helpful for measuring stuff like this.

And obviously, if someone sees a much better solution, I’ve love to hear it.

  • 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-07T03:16:43+00:00Added an answer on June 7, 2026 at 3:16 am

    I find the double-bind method quite elegant – I think it accurately reflects your actual intent, and it only takes two lines of code.

    But another approach is rather than using .one() you could use .on() and update the event object associated with the first event, adding a flag so that the callback will ignore the first time it is called:

    function oneCallback(e) {
        if (e.originalEvent.firstTimeIn)
            return;
    
        alert("This is the one after the current event");
        $(document).off("click", oneCallback);
    }
    
    $("div.source").click(function(e) {
        e.originalEvent.firstTimeIn = true;
        $(document).on("click", oneCallback);   
    });
    

    Demo: http://jsfiddle.net/q5LG4/

    EDIT: To address your concerns about not modifying the event object (or any object you don’t own) you could store a firstTime flag in a closure. Here’s a rather dodgy .oneAfterThis() plugin that takes that approach:

    jQuery.fn.oneAfterThis = function(eventName, callback) {
        this.each(function() {
            var first = true;
            function cb() {
                if(first){
                    first = false;
                    return;
                }
                callback.apply(this,[].slice.call(arguments));
                $(this).off(eventName,cb);
            }
            $(this).on(eventName, cb);
        });
    };
    
    $(someseletor).oneAfterThis("click", function() { ... });
    

    I’m sure that could’ve done that more elegantly (perhaps I should’ve bothered to look at how jQuery implements .one()), but I just wanted to whip something up quickly as a proof of concept.

    Demo: http://jsfiddle.net/q5LG4/1/

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

Sidebar

Related Questions

I have a situation where I need to use some strings temporarily but I've
i have a situation where i need to use GPS technique. i need to
In my view, if I have a situation where I need to use a
I have a situation where in theory I would need to use a belongs_to_many
I have a situation in which i need a reference to the very next
I'm a CSS/JQuery rookie, but have built several things with the basics. Just need
I have a weird situation (don't we all?) with datepickers and want to get
This is the situation, I have 2 div's next to each other. One div
I have a situation where I need to use a huge number for the
I have a situation where I need to use both EdgeNGramFilterFactory and NGramFilterFactory. I

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.