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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:21:35+00:00 2026-05-29T12:21:35+00:00

why is this returning undefined for all ID’s? var time = 200; $(‘.block’).each(function ()

  • 0

why is this returning undefined for all ID’s?

var time = 200;

$('.block').each(function () { 
    setTimeout(function () {
        console.log($(this).attr('id'));
        }, time);

    time += 200;
});

but this returns the ID’s just fine?

var time = 200;

$('.block').each(function () { 
    console.log($(this).attr('id'));

    setTimeout(function () {
        }, time);

    time += 200;
});

I’m trying to create a sort of bubbling in one at a time effect and this is driving me crazy

  • 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-29T12:21:36+00:00Added an answer on May 29, 2026 at 12:21 pm

    The value of this inside the callback (the global object) is not the same as in the outer scope (where it’s the current element). Use the following to close over a reference to the correct value:

    $('.block').each(function () { 
        var that = $(this);
        setTimeout(function () {
            console.log(that.attr('id'));
        }, time);
        time += 200;
    });
    

    This works because the function passed to setTimeout is a closure that retains access to the variables declared in its lexical scope (which includes that).

    The general lesson here is that the value of this inside a function is determined dynamically and depends entirely on how the function was executed:

    • jQuery executes the outer function and explicitly sets this to the current element (using call/apply).
    • The browser executes the inner function in a global context, which is why the value of this is window there. This simple test demonstrates that last point:

      setTimeout(function() {
          alert(this === window); // true
      }, 1000)
      

    In modern browsers, an alternative to the solution above is to use bind:

    $('.block').each(function () { 
        setTimeout(function () {
            console.log($(this).attr('id'));
        }.bind(this), time);
        time += 200;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question. Why is this JavaScript function returning undefined? var redis = require(redis), client
This Code: Something = new Guid() is returning: 00000000-0000-0000-0000-000000000000 all the time and I
This is returning undefined , even though var roommate contains the correct value: JS
i got this code that keep on returning undefined msg instead of the expected
$(#<%=ApprovalSelectPanel.ClientID %> input:checkbox:checked).each(function(){ alert(this.val); }); This isn't returned the value attribute on each checkbox,
Can anyone tell me why this code is returning undefined . I was hoping
https://stackoverflow.com/questions/5282437/cucumber-pickle-factory-girl-given-a-returning-undefined-step - this post didn't help me a lot. Gemfile: gem 'rails' # 3.0.5
how can I prevent PHP from returning an Undefined variable error every time I
When I serialize an object which has a DateTime in it this is returning
I am writing a query in SQL server2005. This is returning me a duplicate

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.