473,513 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can i use external variables in "setInterval" 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 11175
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
5309
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
15191
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...
1
3467
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...
2
1238
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"...
3
2476
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>...
4
2606
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...
2
1399
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...
7
2218
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...
17
1581
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. ...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7527
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4746
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...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3223
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1597
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 ...
1
803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.