setTimeOut Arrgument Passing [英] setTimeOut Arrgument Passing

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

问题描述

在JavaScript中我想使用 setTimeOut()这样的函数

In JavaScript I want to use the setTimeOut() function like this

<script>    
var id=12;

setTimeOut("showGrid('i want to pass variable id here')",5000);

</script>


推荐答案

最好的方法是通过匿名功能到setTimeout。这个匿名函数将能够访问 id

The best way to do this would be to pass an anonymous function to setTimeout. This anonymous function will be able to access id

setTimeout(function() { showGrid(id); }, 5000);

将字符串传递给 setTimeout (而不是函数)通常被认为是邪恶的,因为字符串将是 eval 'd,应该避免。

Passing a string to setTimeout (instead of a function) is usually considered evil, since the string will be eval'd, and should be avoided.

另请注意,您的代码中有一个小错字:函数是 setTimeout ,而不是 setTimeOut (注意小写 o

Also note that you had a slight typo in your code: the function is setTimeout, not setTimeOut (note the lowercase o)

编辑

根据您的评论,代码如下所示:

Based on your comment, the code would look like this:

setTimeout(function() { document.getElementById().inerHTML = data; }, 500);

除了你需要将某种id传递给 document.getElementById

except of course you need to pass some sort of id to document.getElementById

这篇关于setTimeOut Arrgument Passing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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