Is there any way to print out the value of the array players
like in the example below? I've tried to find a solution for hours now...
function Room(name, id, owner) {
this.players = [];
this.movementz = function() {
console.log(this.players);
}
}
I'm calling the function using setInterval
, like this:
setInterval(room.movementz, 1000);
This is unlikely to make much of a difference though, and as has been mentioned, using setInterval with long intervals (a second is big, 4ms is small) is unlikely to have any major effects.
In order to understand why setInterval is evil we need to keep in mind a fact that javascript is essentially single threaded, meaning it will not perform more than one operation at a time.
Nested setTimeout calls are a more flexible alternative to setInterval , allowing us to set the time between executions more precisely. Zero delay scheduling with setTimeout(func, 0) (the same as setTimeout(func) ) is used to schedule the call “as soon as possible, but after the current script is complete”.
Yes, setInterval repeats until you call clearInterval with the interval to stop. By way of example, the following code will count to 5. setInterval sets up the counter, setTimeout sets up a single event in 5 seconds to stop the counter, and clearInterval stop counting.
The problem here is about the this
object: creating your object and manually calling it's movementz
method will work because the this
element is the object itself, but using setInterval
will cause the method to be called with this === window
.
Here is an example:
var room = new Room();
room.movementz(); // []
setInterval(room.movementz, 1000); // undefined
This happens because when the movementz
method gets called by setInterval
, the this
object is window
, so, to fix this, you'll have to force the function to be called using room
as this
. You can easily accomplish this using the bind
method, here's an example:
var room = new Room(),
players = "hello";
setInterval(room.movementz, 1000);
// this will output "hello" because this === window
setInterval(room.movementz.bind(room), 1000);
// this will output [], because now this === room
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With