第一个javascript函数调用从未执行 [英] first javascript function call never executed

查看:68
本文介绍了第一个javascript函数调用从未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有脚本标签的Grails视图,如下所示:

I have a Grails view with script tag like this:

<script type="text/javascript">

        /* the first setInterval */
    setInterval(
      function()
      {
        console.log("Test...");
      },
      5000
    );

    setInterval(
      function()
      {
        console.log("Test... (2)");
      },
      5000
    );

    GetContent(1);
    GetContent(2);
    GetContent(3);
    GetContent(4);

    setInterval(
      Test0,
      5000
    );

    setInterval(
      Test2,
      5000
    );

    setInterval(
      ShowTime,
      1000 * 10
    );

    setInterval(
      Test3,
      1000 * 9
    );

    setInterval(
      Test1,
      1000 * 8
    );

</script>

我想知道,为什么第一个setInterval从未执行过? 不知何故,第一个javascript函数调用从未执行.函数调用显示在html(查看源代码)中,但从未执行.

I wonder, why the first setInterval never executed? Somehow, the first javascript function call never executed. The function call shows up in the html (view source), but it never executed.

如果我复制第一个setInterval并将其粘贴在第一个setInterval的正下方,那么我有2个相同的函数调用,只有其中一个被执行.

If I copy the first setInterval and paste it right underneath the first one, so I have 2 identical function call, only one of them get executed.

控制台每5000毫秒只编写一次"Test ...".

The console only write "Test..." once each 5000 milliseconds.

这是Grails中的错误吗?

Is it a bug in Grails?

推荐答案

我的问题的答案仅仅是将那些javascript函数调用绑定到文档的ready事件.

The answer to my question is simply to bind those javascript function calls to document's ready event.

<script type="text/javascript">


$(document).ready(
  function()
  {

    /* the first setInterval */
    setInterval(
      function()
      {
        console.log("Test...");
      },
      5000
    );

    setInterval(
      function()
      {
        console.log("Test... (2)");
      },
      5000
    );

    GetContent(1);
    GetContent(2);
    GetContent(3);
    GetContent(4);

    setInterval(
      Test0,
      5000
    );

    setInterval(
      Test2,
      5000
    );

    setInterval(
      ShowTime,
      1000 * 10
    );

    setInterval(
      Test3,
      1000 * 9
    );

    setInterval(
      Test1,
      1000 * 8
    );

  };

</script>

现在,一切按预期执行.

Now, everything executed as expected.

这篇关于第一个javascript函数调用从未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆