1

I have this code which calls a function test() on body onload

<body onLoad="test();">

The Test function has 2 more functions drawLayers() ,StopAll().

function test() {
 function drawLayers() {
 timers = [];
 timers.push(setTimeout(drawMoon,800));
 timers.push(setTimeout(drawCircle1,2300));
 timers.push(setTimeout(drawCircle2,2700));
 timers.push(setTimeout(drawCircle3,3100));
 timers.push(setTimeout(drawCircle4,3500));
 timers.push(setTimeout(drawCircle5,3900));
 timers.push(setTimeout(drawtext2,4300));
 timers.push(setTimeout(drawtext,4700));
 timers.push(setTimeout(drawtext3,5100));
 timers.push(setTimeout(drawtext4,5500));
 timers.push(setTimeout(drawtext5,5900));
 timers.push(setTimeout(drawtext6,6300));
 timers.push(setTimeout(drawtext7,6700));
 timers.push(setTimeout(drawtext8,7100));
 timers.push(setTimeout(drawtext9,7500));
 timers.push(setTimeout(drawtext10,7900));
 }
 function StopAll() {
 alert('fsdfsdf');
 for (var i = 0; i < timers.length; i++)
 window.clearTimeout(timers[i]);
 }
}

What i want to do is Call the StopAL() function on click of a button, the html code looks like below

<a href="javascript:void(0);" onClick="StopAll();">

Its throwing error, "StopAll is not defined"

How do i call the StopALL() function?

asked Aug 27, 2011 at 6:27

7 Answers 7

5

The scope of those nested functions is restricted to the test function only. You cannot invoke them from the outside. If you need to do that you could externalize it from the test function.

answered Aug 27, 2011 at 6:34
4

This is a 'closure' problem. The function StopAll is within the scope of the test function, and therefore is undefined in the global scope in which you are trying to call it.

Closures are a tricky subject to grasp initially. There's a good explanation here: How do JavaScript closures work?

(by the way StopAll should really be called stopAll because capitalised functions are generally reserved for use with the new keyword.)

answered Aug 27, 2011 at 6:38
3
test = function (){
 this.drawLayers = function() {
 this.timers = [];
 this.timers.push(setTimeout(drawMoon,800)); 
 }
 this.StopAll = function() {
 alert('fsdfsdf');
 var t = timers.length
 for (var i = 0; i < t; i++)
 window.clearTimeout(this.timers[i]);
 }
}
 var testObj = new test();
 testObj.StopAll()
answered Jul 4, 2012 at 8:36
1
  • 2
    You should add a description to your answer, rather than just posting code. Commented Oct 11, 2012 at 5:01
2
function test() {
 function drawLayers() {
 timers = [];
 timers.push(setTimeout(drawMoon,800));
 timers.push(setTimeout(drawCircle1,2300));
 timers.push(setTimeout(drawCircle2,2700));
 }
 var StopAll=function() {
 alert('fsdfsdf');
 for (var i = 0; i < timers.length; i++)
 window.clearTimeout(timers[i]);
 }
 return StopAll;
}
var obj= new test();
//to call StopAll function
obj();
answered Mar 3, 2015 at 22:05
1
  • it works good but i takes 2 sec to execute that func. too late Commented Mar 29, 2015 at 15:48
1
(function test($) {
 function drawLayers() { 
 }
 //expose this to outside world ,public function
 $.StopAll = function() {
 alert('fsdfsdf');
 }
})(window);
StopAll();
answered Jun 28, 2012 at 5:41
0

You'd better not use html attributes to bind event handler, you can do the same with the following code:

 window.onload = function(){
 document.getElementById("myLink").onclick = function(){
 StopAll(); 
 } 
 }
 // Your functions

This way you'll ensure your dom is loaded and ready to call event handlers.

answered Aug 27, 2011 at 6:39
0

You can move the function StopAll() outside the test function and call it as specified. If suppose you need to access that function even in the test(), you can do like this

function test() {
.....
drawLayers();
StopAll() ;
}
function StopAll() {
 alert('fsdfsdf');
 for (var i = 0; i < timers.length; i++)
 window.clearTimeout(timers[i]);
 }

Declaration of function can be given outside and called any where you want

answered Aug 27, 2011 at 6:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.