SetTimeout中的Javascript变种

use*_*398 2 javascript jquery settimeout

问题很简单,我需要在SetTimeout中插入var,以便在重定向到url之后,但问题是从var中取值redir:

    <script>
/// Var for redirect 
        var redir="action";
/// Set Time for redirect with the until var inside url
        setTimeout("location.href='?action=cp&load='+action",1500);
    </script>
Run Code Online (Sandbox Code Playgroud)

问题对我来说是引号,因为我已经为SetTimeout设置了这些引号并且为了放置var redir的值我不知道究竟放在哪里

谢谢

Tra*_*s J 7

最好在setTimeout调用中使用匿名函数.这是因为将Function使用与使用类似的构造函数构造字符串eval.此外,使用匿名函数将允许您使用更简单的方法来连接字符串.

setTimeout(function(){
 location.href = '?action=cp&load=' + action;
},1500);
Run Code Online (Sandbox Code Playgroud)