---
title: "javaScript定时器中动态传递参数且可延时"
author: "hwei199"
date: "2015-12-29T17:49:32Z"
tags: []
categories: ["Web前端"]
slug: "javascript"
draft: false
lastmod: "2015-12-29T17:49:32Z"
id: 2267470
url: "https://hwei199.iteye.com/blog/2267470"
---

javascript中的setTimeout(code,millisec) 大家也许常用，但或许会有以下的困扰：
1、明明指定了延时，但达不到延时效果，像没指定一样
你会发现将函数名加上引号会达到效果：

```java
setTimeout("func()",millisec) 
```

但对于需要传递参数的函数，那就没办法了。

2、如果又需要传递参数，又要延时执行，那么可以采用在临时函数中返回实际函数的做法：

```java

setTimeout(_imgSelSleep(file),500);

function _imgSelSleep(file)  ////这个是临时函数
{  
    return function()  
    {  
    	imgSelSleep(file);  //这个是实际函数
    }  
}  
```