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

  • Home
  • SEARCH
  • 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 8835789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:20:26+00:00 2026-06-14T09:20:26+00:00

I do not understand what is wrong. I have three codes: First: <script language=JavaScript

  • 0

I do not understand what is wrong. I have three codes:

First:

<script language="JavaScript" type="text/javascript">
   var count = 0;
   alert(count);
   var timer = setInterval("count = count + 1; alert(count);",10000);
</script>

Second:

<script language="JavaScript" type="text/javascript">
  function countdown()
  {
   var count = 0;
   alert(count);
   var timer = setInterval("count = count + 1; alert(count);",10000);
  }
  countdown();
</script>

Third:

<script language="JavaScript" type="text/javascript">
   var count = 0;
  function countdown()
  {
   alert(count);
   var timer = setInterval("count = count + 1; alert(count);",10000);
  }
  countdown();
</script>

The first code works fine, the second produces an error in the “setInterval” line: “count is not defined”, and the third code works fine again. The scope of the “count” variable should be global for the setInterval function in the second code. Why is it not? I am using Mozilla Firefox.
Thanks.

  • 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-14T09:20:28+00:00Added an answer on June 14, 2026 at 9:20 am

    For a great number of reasons, one of which you just ran into, never ever pass a string to setTimeout or setInterval. Ever. I mean it. There is never a good reason.

    Pass a function instead. The ability to pass function objects around is one JS best features.

    var count = 0;
    alert(count);
    
    var timer = setInterval(function(){
      count = count + 1;
      alert(count);
    }, 10000);
    

    The problem you are facing is that code as a string in this manner won’t respect scope. It will execute in the global scope, which is a place your variable doesn’t exist in your 2nd and 3rd snippets. And the first snippet works because count is indeed a global variable.

    Other problems with this stem from the fact this is basically eval which comes with its own headaches and is best to avoid entirely. Eval is Evil after all.

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

Sidebar

Related Questions

I do not understand what is wrong with this query? Query tool does not
I have tried to understand dependency injection and not quite gotten it, except I
First off i am not all that familiar with the OOP side of Javascript
I simply could not understand why the following code does not work. What could
I do not understand why this error occurs...Is this a bug in XE, or
I do not understand pointers. Where can I learn more about them?
I simply do not understand why both works: this.timer.Tick += new EventHandler(timer_Tick); this.timer.Tick +=
I do not understand what environment a eval or exec statement executes in. You
I do not understand this error, do not generate error in JsonResult Test (),
I do not understand how to use enumeration types. I understand what they are,

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.