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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:31:11+00:00 2026-06-06T18:31:11+00:00

I found a bug, and tracked it down. You can see a simplified example

  • 0

I found a bug, and tracked it down.
You can see a simplified example of my code here.

As it turns out, I need to debounce my if() statement rather than debouncing the function itself.
I’d like to keep the debounce as a standalone function, but I’m not sure then how to pass the conditional in.

Any pointers?

Here’s the code:

var foo = function(xyz) {
    alert(xyz);
};

function setter(func, arg1, arg2) {
    return {
        fn: func,
        arg1: arg1,
        arg2: arg2
    };
}

function debounce(someObject) {
    var duration = someObject.arg2 || 100;
    var timer;
    if (timer) {
        clearTimeout(timer);
    }
    timer = setTimeout(function() {
        someObject.fn(someObject.arg1);
        timer = 0;
    }, duration);
}

var toggle = true;

if (toggle) {
    debounce(setter(foo, 'The best things in life are worth waiting for.', 1250));
} else {
    foo('Instant gratification is sweet!!');
}
  • 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-06T18:31:12+00:00Added an answer on June 6, 2026 at 6:31 pm

    Using your example, why not pass toggle in as arg 1… something like:

    var toggle = true;
    var debouncedFunk = function(toggle) {
      if (toggle)
        // the function call
      else
        // something else
    };
    debounce(debouncedFunk, toggle, 1250);
    

    You should also look into using the Function objects .call and .apply methods. They are for calling the function and passing in arguments. Taking the example function:

    var example = function(one, two) { 
      // Logic here
    };
    

    You can call it in three ways:

    // First
    example(1, 2);
    // Second
    example.call({}, 1, 2);
    // Third
    example.apply({}, [ 1, 2 ]);
    

    The first is the standard way to call a function. The difference between the first and the .call is that the first parameter to .call is the context object of the function (what this will point to inside the function), the other parameters are passed after that (and a known list is required for .call. The benefit of .apply is that you can pass an array to the function of arguments and they will be assigned to the parameter list appropriately, the first parameter is still the context object.

    It would simplify your debounce function, instead of having to deal with a structured object as you currently do.

    A suggestion for your debounce:

    var debounce = function(funk, delay) {
      var args = [];
      if (arguments.length > 2)
        args = [].slice.call(arguments, 2);
      setTimeout(function() { funk.apply({}, args); }, delay);
    };
    

    Changing your current if to:

    var toggle = true;
    var debouncedFunk = function(toggle) {
      if (toggle)
        // Do if true
      else
        // DO if false
    };
    debounce(debouncedFunk, 1000, toggle);
    

    Maybe too much information (sorry)?

    As a last note, I’d recommend using a framework (if possible) where these functions have been implemented already (and many other useful functions) such as Underscore. Using Underscore your example would look like:

    // Define debouncedFunk and toggle
    debouncedFunk = _.bind(debouncedFunk, {}, toggle);
    debouncedFunk = _.debounce(debouncedFunk, 1000);
    debouncedFunk();
    

    EDIT

    Fixed the underscore example, _.debounce returns a function that will execute only after the delay but it still needs to be called.

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

Sidebar

Related Questions

I found a bug in my code which boiled down to comparing Double(0.0) with
I tracked down a very weird ... bug... I found that for some reason,
I found a bug in code (the if statement should have had == instad
We've found a bug in old code where connections aren't being closed. It's an
Just found a bug in this code : for(String link : tempList1){ if(!tempList2.contains(link));{ listToPopulate.add(link);
I have found a very subtle bug in my R code just now. The
I found a bug in my JavaScript code that I have isolated to a
I found a bug in my code where I compared the pointer with '\0'.
I just found a bug in some code I didn't write and I'm a
Yesterday I tracked down a strange bug which caused a website display only 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.