JS setTimeout() 传参

来源:互联网 发布:sqlserver union 编辑:程序博客网 时间:2024/06/01 07:26

setTimeout()  使用方式参考w3school。

setTimeout 使用方式: var t = setTimeout("javascript statement", milliseconds);


实例:

setTimeout("hello"1000); // 每1秒執行一次 hello().

setTimeout("hello()"1000); // 每1秒執行一次 hello().

setTimeout 带参数:

1.网上有一种字符串拼接的方法: setTimeout("hello(" + value + ")", 1000); 经测试只有传递非字符时有效。

<html> <head> <meta http-equiv="contentType" content="text/html;charset=utf-8"> <script src="http://code.jquery.com/jquery-latest.js"></script> <script>function exec_func(value){alert(value);setTimeout("exec_func(" + value + ")", 3000);//如果参数为字符串的话在第二次执行时字符串为变量,会报未定义的错误。}$(document).ready(function(){exec_func(1);exec_func("hello"); //这句话会报错,错误为 hello未定义。});</script> </head> <body> <div></div></body> </html>


2.修改后的方法(个人推荐):

<html> <head> <meta http-equiv="contentType" content="text/html;charset=utf-8"> <script src="http://code.jquery.com/jquery-latest.js"></script> <script>function exec_func(value){alert(value);setTimeout(function(){exec_func(value)}, 3000);}$(document).ready(function(){exec_func('Hi,Gril');});</script> </head> <body> <div></div></body> </html>


0 0