setInterval 的问题

发布于 2024-10-15 05:41:03 字数 261 浏览 8 评论 0原文

我只是想用 jQuery 实现 setInterval 的最基本示例,但遇到了问题。这是怎么回事?

它说函数旋转未定义。

$(document).ready(function() {
    var speed = 5000;
    var run = setInterval("rotate()", speed);

    function rotate() {
        alert ('rotate');
    }
});

I am just trying to implement the most basic example of setInterval with jQuery and am having problems. What's wrong here?

It says function rotate is not defined.

$(document).ready(function() {
    var speed = 5000;
    var run = setInterval("rotate()", speed);

    function rotate() {
        alert ('rotate');
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

天冷不及心凉 2024-10-22 05:41:03

这样做:

$(document).ready(function() {
    var speed = 5000;

    function rotate() {
        alert ('rotate');
    }

    var run = setInterval(rotate, speed);
});

您也可以简单地这样做:

$(function() {
    var speed = 5000;

    var run = setInterval(function() {
        alert ('rotate');
    }, speed);
});

如果您希望稍后能够清除间隔,则应该在就绪事件处理程序之外声明 run ,否则它将超出范围。

Do this instead:

$(document).ready(function() {
    var speed = 5000;

    function rotate() {
        alert ('rotate');
    }

    var run = setInterval(rotate, speed);
});

You could also simply do this:

$(function() {
    var speed = 5000;

    var run = setInterval(function() {
        alert ('rotate');
    }, speed);
});

You should declare run outside the ready event handler if you wish to be able to clear the interval later though, since it will go out of scope otherwise.

梦醒灬来后我 2024-10-22 05:41:03

您正在将 setInterval 字符串传递给 eval。这是:

  • 难以调试
  • 低效
  • 丑陋
  • 在不同范围内执行

该函数的范围仅限于其内部定义的函数。

直接传递函数即可

    var run = setInterval(rotate, speed);

You are passing setInterval a string to eval. This is:

  • Hard to debug
  • Inefficient
  • Ugly
  • executed in a different scope

The function is limited in scope to the function it is defined inside.

Pass the function directly instead

    var run = setInterval(rotate, speed);
山川志 2024-10-22 05:41:03

在 setInterval 调用中省略旋转的括号和引号。您给它的是函数本身,而不是名称。他们进入的顺序应该不重要。

omit the parenthesis and quotes around rotate in your setInterval call. You are giving it the function itself, not the name. It shouldn't matter the order that they go in.

那伤。 2024-10-22 05:41:03

这实际上是在(我认为是)它自己的上下文中进行轮换评估。改成这样

var run = setInterval(rotate, speed);

应该就可以了。

That's effectively eval-ing rotate in (what I believe is) its own context. Change it to

var run = setInterval(rotate, speed);

and it should be fine.

梦亿 2024-10-22 05:41:03

或者甚至将旋转函数放在就绪函数之外。

or even put the rotate function outside the ready function.

一场春暖 2024-10-22 05:41:03

这有效

$(document).ready(function() {
    function rotate() {
        alert ('rotate');
    }
    var speed = 5000;
    var run = setInterval(function() { rotate() }, speed);

});

this works

$(document).ready(function() {
    function rotate() {
        alert ('rotate');
    }
    var speed = 5000;
    var run = setInterval(function() { rotate() }, speed);

});
灯下孤影 2024-10-22 05:41:03
$(function() {
   var speed = 5000;
   var rotate = function() {
      alert("rotate");
   };

   window.setInterval(rotate, speed);
});
$(function() {
   var speed = 5000;
   var rotate = function() {
      alert("rotate");
   };

   window.setInterval(rotate, speed);
});
寂寞笑我太脆弱 2024-10-22 05:41:03

您需要在全局范围内定义rotate

$(document).ready(function() {
    window.rotate = function() {
        alert ('rotate');
    }
    var speed = 5000;
    var run = setInterval("rotate()", speed);
});

You need to define rotate in the global scope.

$(document).ready(function() {
    window.rotate = function() {
        alert ('rotate');
    }
    var speed = 5000;
    var run = setInterval("rotate()", speed);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文