使用javascript调用嵌套函数

使用javascript调用嵌套函数,javascript,Javascript,我有一段代码,在body onload上调用函数测试 <body onLoad="test();"> 测试函数还有另外两个函数drawLayers,StopAll function test() { function drawLayers() { timers = []; timers.push(setTimeout(drawMoon,800)); timers.push(setTimeout(drawCircle1,2300));

我有一段代码,在body onload上调用函数测试

<body onLoad="test();">
测试函数还有另外两个函数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]);
    }
}
我想做的是单击按钮调用StopAL函数,html代码如下所示

<a href="javascript:void(0);" onClick="StopAll();">
未定义其抛出错误StopAll


如何调用StopALL函数?

这些嵌套函数的作用域仅限于测试函数。您不能从外部调用它们。如果需要这样做,可以从测试函数中将其外部化。

这些嵌套函数的范围仅限于测试函数。您不能从外部调用它们。如果需要这样做,可以从测试函数中将其外部化。

这是一个“闭包”问题。函数StopAll在测试函数的作用域内,因此在您试图调用它的全局作用域中未定义

闭包是一个很难掌握的问题。这里有一个很好的解释:


顺便说一句,StopAll实际上应该被称为StopAll,因为大写的函数通常保留用于new关键字。

这是一个“闭包”问题。函数StopAll在测试函数的作用域内,因此在您试图调用它的全局作用域中未定义

闭包是一个很难掌握的问题。这里有一个很好的解释:


顺便说一句,StopAll实际上应该被称为StopAll,因为大写的函数通常保留用于new关键字。

您最好不要使用html属性绑定事件处理程序,您可以对以下代码执行相同的操作:

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


  // Your functions

通过这种方式,您将确保加载dom并准备好调用事件处理程序。

您最好不要使用html属性绑定事件处理程序,您可以使用以下代码执行相同的操作:

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


  // Your functions

通过这种方式,您将确保加载dom并准备好调用事件处理程序。

您可以将函数StopAll移到测试函数之外,并按指定调用它。如果假设您需要在测试中访问该函数,您可以这样做

function test() {

.....
drawLayers();
StopAll() ;

}

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

函数声明可以在外部给出,并在任何需要的地方调用

您可以将函数StopAll移到测试函数外部,并按指定调用它。如果假设您需要在测试中访问该函数,您可以这样做

function test() {

.....
drawLayers();
StopAll() ;

}

function StopAll() {
     alert('fsdfsdf');
        for (var i = 0; i < timers.length; i++)
             window.clearTimeout(timers[i]);
    }
(function test($) {
  function drawLayers() {  
  }
  //expose this to outside world ,public function
  $.StopAll = function() {
    alert('fsdfsdf');
  }
})(window);

StopAll();

函数声明可以在外部给出,可以在任何地方调用。

您应该在答案中添加说明,而不仅仅是发布代码。您应该在答案中添加说明,而不仅仅是发布代码。它工作正常,但我需要2秒钟来执行该函数。太晚了,它运行良好,但我需要2秒来执行该函数。太晚了
(function test($) {
  function drawLayers() {  
  }
  //expose this to outside world ,public function
  $.StopAll = function() {
    alert('fsdfsdf');
  }
})(window);

StopAll();