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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:07:52+00:00 2026-06-15T17:07:52+00:00

The following code is presented, in the book Head First jQuery . function lightning_one(t)

  • 0

The following code is presented, in the book Head First jQuery.

function lightning_one(t) {
$("#lightning1").fadeIn(250).fadeOut(250);
setTimeout("lightning_one()", t);
}; // end lightning_one

It gets called with this line.

lightning_one(3000);

The observed behaviour is that the lightning fades in and out once, waits 3 seconds, fades in and out again, and then continues to fade in and out. Firebug shows no javascript errors.

I understand why I see what I see. I thought I would attempt to preserve the 3 second interval, so I changed this:

setTimeout("lightning_one()", t);  // nothing in the brackets

to this:

setTimeout("lightning_one(t)", t);  // t is in the brackets

When I refresh the page, the lightning fades in and out once. Firebug tells me that variable t is undefined.

My question is, if the variable t is not defined after my change, how did the command run without error before I changed it? It still has a variable named t.

More Info

Thank you everyone who wrote comments and answers. For the record, in the “end” folder, the code becomes this:

    lightning_one();

   function lightning_one(){
$("#container #lightning1").fadeIn(250).fadeOut(250);
setTimeout("lightning_one()",4000);
    };

I haven’t finished the applicable chapter yet so I don’t know if the code change gets suggested later on. As mentioned earlier, this might not be the best book out there. However it’s the one I bought and I am learning the fundamentals of jQuery from 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-15T17:07:53+00:00Added an answer on June 15, 2026 at 5:07 pm

    Because the first argument of setTimeout as a string is eval-ed, meaning that it will looking into the outer scope of the function in which t is not defined. The first one is fine because you don’t use any variables in the call, but the second is not because t is not defined outside the scope of the function. It is in fact local.

    Advice: Don’t use eval at all. In fact, you don’t need the string argument. Use a function expression:

    setTimeout(function() {
    
        lightning_one(t);
    
    }, t);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code in html file: <html> <head> <title>My Great Website</title> </head> <body>
I'm trying to draw jqplot with the following code: <script lang=javascript type=text/javascript> $(document).ready(function ()
I am just learning WPF and Caliburn.Micro. I am following the code that presented
In the code presented here: Gant file , there is the following code: target(dist:
I call a View to be presented with the following code: #import infoView.h ...
here the following code is used to view the present modal view controller. [[self
I am writing the following code to find out the required strings are present
I have the following code in my model: attr_accessor :expiry_date validates_presence_of :expiry_date, :on =>
Using the R# keyboard shortcut for formatting code presents the following window: which forces
I am trying to delete the data present in the dataset with following code:

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.