473,387 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trouble executing code due to scope restrictions of timeouts/intervals

I am having a problem involving the scope of timeouts and intervals.
Since timeouts and intervals execute in the global scope, dynamically
generated local interval/timeout declarations do not work, because the
dynamically generated code they have to execute involves variables
that have been thrown out by the time of execution. In the first of
the two following code examples, my interval declaration includes code
that relies on variables defined within a local function, and they
have to be defined locallly for the script to work. Basically, as the
second of the two following code examples shows, I am trying to define
20 "bullet" objects and their movement functions dynamically and with
prototypes so that a lot of memory and script file size is not wasted.
So far though, the only way that I can get this to work is statically
defining a separate movement function for each "bullet" object. Each
function is the same except for 3 or 4 places where it refers to
specific bullets—but when I do it dynamically, it cannot work because
of scope restrictions. If someone has an idea of how to work around
this, that'd be awesome.
******************

// when a certain key is pressed, this is executed

var the_bullet ;
var found_one = false ;
for (var i = 1 ; i < 21 ; i++) {
the_bullet = "bullet" + i ;
if (all_bullets[the_bullet]["inuse"] == false) {
all_bullets[the_bullet]["inuse"] = true ;
found_one = true ;
break ;
}
}
if (found_one != true) return false ;

// Here is the important interval declaration

all_bullets[the_bullet].intervalID =
setInterval('all_bullets[the_bullet]["moveBullet"]();', 180) ;

******************


Previously defined in the script file is the following, which creates
20 different bullet objects that are part of the all_bullets object,
and defines a prototype moveBullet function.
*****************

var all_bullets = new Object() ;

function primaryBullet(bullet_name) {
this.name = bullet_name ;
this.intervalID = null ;
}

all_bullets.prototype.moveBullet = function() {
var div_ref = window.document.getElementById(this.name) ;
div_ref.style.left = parseInt(div_ref.style.left) - 0 + 20 + "px" ;
// does other stuff like this
}

for (var i = 1 ; i < 21 ; i++) {
var new_bullet_name = "bullet" + i ;
all_bullets[new_bullet_name] = new primaryBullet(new_bullet_name) ;
}

*****************
Jul 23 '05 #1
4 1344
zm*******@excite.com (zmcelrath87) writes:
I am having a problem involving the scope of timeouts and intervals.
Since timeouts and intervals execute in the global scope,


Only if you use a string argument. In modern browsers, you can use
a function as argument instead:
---
function delayedBlastMessage(s, t) {
setTimeout(function(){alert(s);}, t);
}
delayedBlastMessage("hello world", 1000);
---

It works in Netscape 4, and I think it was introduced to IE in version
5.5.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2
On 11 Jul 2004 12:58:01 -0700, zm*******@excite.com (zmcelrath87)
wrote:
I am having a problem involving the scope of timeouts and intervals.


http://www.jibbering.com/faq/faq_not...res.html#clSto

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 23 '05 #3
> It works in Netscape 4, and I think it was introduced to IE in version
5.5.


Thanks, it definitely works great in modern browsers, like Mozilla,
Netscape, and Safari, but I was trying to stick with IE 5.2 for MacOSX
compatible code, since my whole project is based on IE's event model,
because, unlike the DOM Level 2 standard, it does include a method of
capturing ordinary keypress events (keys other than modifiers [like
option/command/ctrl] for mouseclick events). Is there any other way to
work around my problem besides using closures?
Jul 23 '05 #4
zm*******@excite.com (zmcelrath87) writes:
Is there any other way to work around my problem besides using
closures?


Accessing local variables outside the function where they live ... no.
That takes closures.

What you can do is to generate some unique, globally accessible
variables containing the closure, and call it using a string argument
to setTimeout:
---
var uniqueNum = 0;
function mySetTimeout(closure, time) {
var key = "key" + (uniqueNum++);
mySetTimeout[key] = closure;
return setTimeout("mySetTimeout[\"" + key + "\"]();"+
"delete mySetTimeout[\"" + key + "\"];", time);
}
---

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Strøiman | last post by:
Hi. I have a web application that needs to run tasks at regular intervals. E.g. it must send out emails every night to people who subscribe to that service. I have come up with one solution,...
1
by: Arnaud Diederen | last post by:
Hello everyone, I have a little problem with IE6. Imagine I have a webapp that has a main/root/parent window, in which there is a function (called: executor()) that makes use of the...
1
by: eyal.susser | last post by:
Hi, I'm trying to write a script that uses diffxml along with 4xupdate to merge XML files. So far, I'm having trouble with even the most basic files. I have tried different versions of the...
3
by: weston | last post by:
I'm making a foray into trying to create custom vertical scrollbars and sliders, and thought I had a basic idea how to do it, but seem to be having some trouble with the implementation. My...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
5
by: Steven T. Hatton | last post by:
This note appears in the discussion of name hiding and uniqueness: §3.3 #4 This note is item #6 in the discussion of "Point of declaration" §3.3.1 #6 What exactly do these statements mean?...
0
by: Christian Welzel | last post by:
Hi there, are there restrictions in connection number or something when connecting to a db2 express c luw through ssl? i have a strange problem: my db2 server is in the internet and configured...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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
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...

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.