Trouble executing code due to scope restrictions of timeouts/intervals

Z

zmcelrath87

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) ;
}

*****************
 
L

Lasse Reichstein Nielsen

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
 
Z

zmcelrath87

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

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?
 
L

Lasse Reichstein Nielsen

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top