Javascript setInterval不起作用 [英] Javascript setInterval not working

查看:123
本文介绍了Javascript setInterval不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要每10秒运行一次javascript函数。

I need to run a javascript function each 10 seconds.

我理解语法必须像以下一样工作但我没有取得任何成功:

I understand the syntax must work like follow but I am not getting any success:

function funcName() {
    alert("test");
}

var func = funcName();
var run = setInterval("func",10000)

但这不起作用。有什么帮助吗?

But this isn't working. Any help?

推荐答案

很多其他答案都集中在一个有效的模式上,但他们的解释并不是很真实彻底了解您当前代码无效的原因。

A lot of other answers are focusing on a pattern that does work, but their explanations aren't really very thorough as to why your current code doesn't work.

您的代码,供参考:

function funcName() {
    alert("test");
}

var func = funcName();
var run = setInterval("func",10000)

让我们把它分成几块。你的函数 funcName 没问题。请注意,当您调用 funcName (换句话说,运行它)时,您将警告test。但请注意 funcName() - 括号表示调用或运行该函数 - 实际上并不返回值。当函数没有返回值时,它默认为一个名为 undefined 的值。

Let's break this up into chunks. Your function funcName is fine. Note that when you call funcName (in other words, you run it) you will be alerting "test". But notice that funcName() -- the parentheses mean to "call" or "run" the function -- doesn't actually return a value. When a function doesn't have a return value, it defaults to a value known as undefined.

调用函数时,将其参数列表附加到括号中的末尾。如果没有任何参数来传递函数,则只需添加空括号,如 funcName()。但是当你想要引用函数本身而不是调用它时,你不需要括号,因为括号表示要运行它。

When you call a function, you append its argument list to the end in parentheses. When you don't have any arguments to pass the function, you just add empty parentheses, like funcName(). But when you want to refer to the function itself, and not call it, you don't need the parentheses because the parentheses indicate to run it.

所以,当你说:

var func = funcName();

您实际上是在声明变量 func 的值为 funcName()。但请注意括号。 funcName()实际上是 funcName 的返回值。如上所述,由于 funcName 实际上并未返回任何值,因此默认为 undefined 。换句话说,您的变量 func 实际上将具有值 undefined

You are actually declaring a variable func that has a value of funcName(). But notice the parentheses. funcName() is actually the return value of funcName. As I said above, since funcName doesn't actually return any value, it defaults to undefined. So, in other words, your variable func actually will have the value undefined.

然后你有这一行:

var run = setInterval("func",10000)

函数 setInterval 有两个参数。第一个是经常运行的函数,第二个是每次运行函数之间的毫秒数。

The function setInterval takes two arguments. The first is the function to be ran every so often, and the second is the number of milliseconds between each time the function is ran.

然而,第一个参数确实应该是一个函数,而不是一个字符串。如果它是一个字符串,那么JavaScript引擎将在该字符串上使用 eval 。换句话说,你的setInterval正在运行以下JavaScript代码:

However, the first argument really should be a function, not a string. If it is a string, then the JavaScript engine will use eval on that string instead. So, in other words, your setInterval is running the following JavaScript code:

func
// 10 seconds later....
func
// and so on

然而, func 只是一个变量(值 undefined ,但这有点无关紧要)。所以每十秒钟,JS引擎会计算变量 func 并返回 undefined 。但这并没有真正做任何事情。我的意思是,它在技术上每10秒进行一次评估,但你不会看到任何影响。

However, func is just a variable (with the value undefined, but that's sort of irrelevant). So every ten seconds, the JS engine evaluates the variable func and returns undefined. But this doesn't really do anything. I mean, it technically is being evaluated every 10 seconds, but you're not going to see any effects from that.

解决方案是给 setInterval 一个运行而不是字符串的函数。因此,在这种情况下:

The solution is to give setInterval a function to run instead of a string. So, in this case:

var run = setInterval(funcName, 10000);

请注意,我没有给它 func 。这是因为 func 在您的代码中不是一个函数;它的值是 undefined ,因为你已经为它指定了 funcName()。就像我上面说的那样, funcName()将调用函数 funcName 并返回函数的返回值。由于 funcName 不返回任何内容,因此默认为 undefined 。我知道我已经多次说了,但这确实是一个非常重要的概念:当你看到 funcName()时,你应该想到<$的返回值 C $ C>了funcName 。当你想引用函数本身时,就像一个单独的实体一样,你应该不用括号,所以你不要调用它: funcName

Notice that I didn't give it func. This is because func is not a function in your code; it's the value undefined, because you assigned it funcName(). Like I said above, funcName() will call the function funcName and return the return value of the function. Since funcName doesn't return anything, this defaults to undefined. I know I've said that several times now, but it really is a very important concept: when you see funcName(), you should think "the return value of funcName". When you want to refer to a function itself, like a separate entity, you should leave off the parentheses so you don't call it: funcName.

因此,您的代码的另一个解决方案是:

So, another solution for your code would be:

var func = funcName;
var run = setInterval(func, 10000);

然而,这有点多余:为什么要使用 func 而不是 funcName

However, that's a bit redundant: why use func instead of funcName?

或者你可以通过修改两位来尽可能保持原始代码的真实性:

Or you can stay as true as possible to the original code by modifying two bits:

var func = funcName;
var run = setInterval("func()", 10000);

在这种情况下,JS引擎将评估 func()每十秒钟。换句话说,它会每隔十秒警告test。然而,正如着名的短语所述, eval 是邪恶的,所以你应尽量避免使用它。

In this case, the JS engine will evaluate func() every ten seconds. In other words, it will alert "test" every ten seconds. However, as the famous phrase goes, eval is evil, so you should try to avoid it whenever possible.

此代码的另一个转折是使用匿名功能。换句话说,一个没有名字的函数 - 你只需将其放在代码中,因为你不关心它的名称。

Another twist on this code is to use an anonymous function. In other words, a function that doesn't have a name -- you just drop it in the code because you don't care what it's called.

setInterval(function () {
    alert("test");
}, 10000);

在这种情况下,由于我不关心函数的调用,我只留下一个通用的,无名(匿名)功能。

In this case, since I don't care what the function is called, I just leave a generic, unnamed (anonymous) function there.

这篇关于Javascript setInterval不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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