tags:

views:

60

answers:

5
<script>
window.onload= function(){
var a = document.getElementById('a');
var b = document.getElementById('ct');
setInterval('b.innerHTML = a.duration',1000);
};
</script>
//Second  script
<script>
var a = document.getElementById('a');
var b = document.getElementById('ct');
window.onload= function(){
setInterval('b.innerHTML = a.duration',1000);
};
</script>

Why the first script not working ?. Chrome : Uncaught ReferenceError: b is not defined

+2  A: 

You need to specify a function as argument to the setInterval, you have problem here:

setInterval('b.innerHTML = a.duration',1000);

Should be:

setInterval(function foo(){b.innerHTML = a.duration},1000);
Sarfraz
This cannot be the case, since the error message clearly shows that the code is actually executed.
Scytale
@Scytale: This is an alternative syntax. the text one would run in global scope, the function(){} one would have its own closure.
J-16 SDiZ
If you write your setInterval like this, the anonymous function becomes a closure over the local variables, so it will continue to have access to them even when it's executed later.
Ted Mielczarek
A: 

My guess would be: because you use var on a and b in the first script. This makes the variables local ones in window.onload (instead of global), and the code in setInterval cannot access them.

Remove the var and it should work.

Scytale
Right, but what is the different of VAR and NOT VAR?
Snoob
var is the keyword you wrote in “var a = …”. It says “this is a local variable, not a global one”. While it is normally a good thing to use it in functions (in order to keep the global namespace clean), in this case it keeps the code in setInterval from finding the variables.
Scytale
Thank ...............
Snoob
What if an eventlistener was already attached to .onload by some other javascript? In the code sample it gets overridden by the assignment.
Jeroen
A: 

You can not reference the documents elements in .onload because the document hasn't been loaded there yet. Move the code to the end of the document just before </body>.

It also avoids problems with multiple event handlers in .onload as you are actually overwriting any pre-existing eventhandlers. Use addEventListener for attaching eventhandlers.

like this:

<body>

// markup

<script>
var a = document.getElementById('a');
var b = document.getElementById('ct');


setInterval('b.innerHTML = a.duration',1000);

</script>

</body>
Jeroen
-1. That is not correct, the document is completely loaded when the onload event is triggered. Besides, putting the code before the end of the body tag would make it execute even earlier.
Guffa
+1  A: 

setInterval runs in the global scope. Any variables you refer to in setInterval that are not accessible from the global scope -- like the local a and b in the first example -- will be undefined at execution time.

Ken Redler
+1  A: 

in the first script "a" and "b" are a variables defined in the scope of the event. The "setInterval" looks for the "innerHTML" property in the document (global) scope. In the second sample "a" and "b" are outside the event definition, i.e. defined directly in the document scope so they are reconized by the "setInterval" function.

Sir Gallahad