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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:02:49+00:00 2026-06-10T22:02:49+00:00

I had these lines in my javascript file and it was working fine. handleInput

  • 0

I had these lines in my javascript file and it was working fine.

handleInput = function(e) {
    var $this = $(this),
    id = $this.attr("id");
    alert(id);
}

....

something.bind("keyup", handleInput);

Then I decided to delay the input function and added following lines:

handleDelayedInput = function(e) {
    setTimeout(handleInput(e), 50);
}

.....

something.bind("keyup", handleDelayedInput);

But now alert(id); says undefined, because I think I couldn’t pass the this to that function.

Am I right? How can I fix? Is there any better way to do that?

  • 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-10T22:02:50+00:00Added an answer on June 10, 2026 at 10:02 pm

    When jQuery calls the event handler it sets the context of the handler function. This isn’t the case when handleInput is called in your second piece of code, so this is set to the default value window.

    You can use apply or call to set the context:

    handleInput.call(this, e);
    

    The difference between the two is that apply takes an array of arguments while you can pass the arguments to call one by one: func.apply(context, arg1, arg2).

    So your full code would be:

    handleInput = function(e) {
        var $this = $(this),
        id = $this.attr("id");
        alert(id);
    }
    
    handleDelayedInput = function(e) {
        var element = this;
        setTimeout(function(){
            handleInput.call(element, e);
        }, 50);
    }
    
    something.bind("keyup", handleDelayedInput);
    

    Note that, since we’re using setTimout we need to find a way to pass the element to the timeout handler function. Also, in your code you’re using the return value of handleInput(e) as the handler function – rather than handleInput.

    I agree with Mike though, if you wrote handleInput yourself and you can modify it there’s not point in using this instead of just passing the argument. If you still want to use handleInput as a direct handler somewhere else it would make sense to keep it the way you have it.

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

Sidebar

Related Questions

Working on a simple video game in JavaScript / HTML5, and I had a
I had this question about arrays in JavaScript. I couldn't really find an answer.
Possible Duplicate: return !1 in javascript In a JavaScript file I had to read
I have a question about placement of javascript. The function nqi_search works fine when
a while back, CNET had these really cool Social Share buttons, that when you
i am facing a new situation. I know someone definitely had faced these type
I had this doubt for sometime now, some people say that there's no such
I am working on a loginMiddleware class for Django. This middleware class must send
I'm stuck trying to get the following javascript to work in IE: var lastMonthBn
I am learning about DOM collections. The textbook I'm using had the following Javascript

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.