473,587 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can i use external variables in "setInterva l" code?

22 New Member
When i run this functions it says the count is undefined.
Expand|Select|Wrap|Line Numbers
  1. function hello(){
  2.     var count = 0;
  3.     var timer = setInterval('count+=1;alert(count);',2000);
  4. }
  5.  
Jul 7 '09 #1
3 11183
Dormilich
8,658 Recognized Expert Moderator Expert
some functions change the variable scope. setInterval() and setTimeout() will have global scope and because count was defined inside a function, count will have the function's local scope and not the global one. thus count is undefined (on global scope).

to work around that you need two things, a function for the code to execute and a closure to preserve the values.

(will be back when I got more time…)
Jul 8 '09 #2
rnd me
427 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. function hello(){
  2.     var count = 0;
  3.     var timer = setInterval( function(){  count+=1;alert(count); },2000);
  4. }
  5.  
Jul 8 '09 #3
Dormilich
8,658 Recognized Expert Moderator Expert
some articles about closures:
JavaScript Closures for Dummies
JavaScript Closures 101
JavaScript Closures

and some about Scope
Scope in JavaScript
Explaining JavaScript Scope and Closures
Jul 8 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
5314
by: Richard A. DeVenezia | last post by:
Dear Experts: Suppose I have global variables: x1, x2, x3 and I have a function that needs to assign a value to a new global variable x4 something like function foo () { count = 0;
6
15202
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I am doing wrong? Your help will be much appreciated. Thanks
1
3471
by: Chris | last post by:
How do I stop displaying "undefined" when using the code below. The problem occurs as I want to display 5 items per page using setInterval but this causes 'mycars' to reach a value that doesn't exist eg. First time through loop would display Ford Vauxhall
2
1246
by: Alex Nitulescu | last post by:
Hi. I have the following HTML (snip): _____________________________________________ <body MS_POSITIONING="GridLayout" onload="Init()"> <form id="frmWebServerAdvanced_1Test" method="post" runat="server"> <div id="service" style="behavior:url(webservice.htc)" onresult="Service_Result()"></div> °Fahrenheit: <input id="txtFahrenheit"> <input...
3
2481
by: car6006 | last post by:
Why am I getting the "undefined" in my txt box, I have tried everything! I am new here so I truly hope you are able to assist me... Thanks <html> <head> <title>Random Proverbs</title> <script type="text/javascript"> <!-- HIDE FROM INCOMPATIBLE BROWSERS quotes = new Array( ); // Initiated NEW ARRAY and also Added ( ) brackets
4
2622
by: dr1ft3r | last post by:
Hey guys, I'm building a site for a landscaping business down the street and can't seem to get part of the code functioning correctly. The code fails on line 68 where I make a reference to an iframe's src property. Being that IE does not follow standard and considers an id, name, etc as a qualifying identifier for the document.getElementById...
2
1403
by: Excel 009 | last post by:
Hi All, I have the appended code which I used for a banner rotator. It works great except that there 1. there is a border which I do not want and 2. on top of the banner there is a black bar with white text "Rotator" which I also do not want. Does anyone know how to get ride of the caption and the border? Excel
7
2227
by: TriAdmin | last post by:
I am working with a system that allow me to add custom fields but I can not add OnChange() language to the custom fields. So I want to have a function in the header that recognizes when fieldx is changed, do a calculation with the value of fieldx and change fieldy. Can that be done without having to add an OnCHange() event to fieldx? If...
17
1593
by: bbkc22113 | last post by:
I need help for some javascript code. I need to make it so that every 7 seconds, "i" will increase by 1. So that I will get " 'i' people have died from hunger since you entered this web page." Eg. Start: 0 people have died from hunger since you entered this web page After 7 seconds: 1 people have died from hunger since you entered this web...
0
7918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.