function tryToDownload(url)
{
oIFrm = document.getElementById('myIFrm');
oIFrm.src = url;
// alert(url);
// url=escape(url);
setTimeout(deletefile(url), 25000);
}
following is deletfile function
以下是deletfile函数
function deletefile(url){
$.ajax({
type:'post',
url: "<%= addToDoDeleteDownloadFile %>",
data:{filename:url},
type : "GET",
timeout : 20000,
dataType : "text",
success : function(data) {
alert("success");
}
});
}
above is my jQuery and i m calling one function at the end after 25 second,but some how it's not delaying the deletefile(url) function and execute just after.So what should be the problem?
上面是我的jQuery,我在25秒后调用一个函数,但是有些函数不会延迟deletefile(url)函数并在之后执行。那么问题应该是什么呢?
2 个解决方案
#1
19
In this line you are calling your function and pass its result to setTimeout()
.
在这一行中,您正在调用函数并将其结果传递给setTimeout()。
setTimeout(deletefile(url), 25000);
If you want to delay the execution, add a wrapper function:
如果您希望延迟执行,请添加包装器函数:
setTimeout( function(){ deletefile(url); }, 25000);
EDIT
编辑
An alternative proposed by @Petah:
@Petah提议的替代方案:
setTimeout(deletefile, 25000, url);
All parameters passed to setTimeout()
after the delay, will be passed to the function at execution. So in this case, you pass the reference to the function, the delay and then the parameter to the function in that order!
延迟之后传递给setTimeout()的所有参数,将在执行时传递给函数。在这种情况下,你将引用传递给函数,延迟,然后参数以这个顺序传递给函数!
Note that according to MDN this way of passing parameters wont work in IE before IE9.
请注意,根据MDN,这种传递参数的方式在IE9之前不会起作用。
#2
3
That's because you are calling the function, and using the return value in the setTimeout call. Wrap it in an anonymous function so that it's called by setTimeout:
这是因为您正在调用函数,并在setTimeout调用中使用返回值。将它封装在一个匿名函数中,这样它就会被setTimeout调用:
function tryToDownload(url) {
oIFrm = document.getElementById('myIFrm');
oIFrm.src = url;
// alert(url);
// url=escape(url);
setTimeout(function() { deletefile(url); }, 25000);
}
#1
19
In this line you are calling your function and pass its result to setTimeout()
.
在这一行中,您正在调用函数并将其结果传递给setTimeout()。
setTimeout(deletefile(url), 25000);
If you want to delay the execution, add a wrapper function:
如果您希望延迟执行,请添加包装器函数:
setTimeout( function(){ deletefile(url); }, 25000);
EDIT
编辑
An alternative proposed by @Petah:
@Petah提议的替代方案:
setTimeout(deletefile, 25000, url);
All parameters passed to setTimeout()
after the delay, will be passed to the function at execution. So in this case, you pass the reference to the function, the delay and then the parameter to the function in that order!
延迟之后传递给setTimeout()的所有参数,将在执行时传递给函数。在这种情况下,你将引用传递给函数,延迟,然后参数以这个顺序传递给函数!
Note that according to MDN this way of passing parameters wont work in IE before IE9.
请注意,根据MDN,这种传递参数的方式在IE9之前不会起作用。
#2
3
That's because you are calling the function, and using the return value in the setTimeout call. Wrap it in an anonymous function so that it's called by setTimeout:
这是因为您正在调用函数,并在setTimeout调用中使用返回值。将它封装在一个匿名函数中,这样它就会被setTimeout调用:
function tryToDownload(url) {
oIFrm = document.getElementById('myIFrm');
oIFrm.src = url;
// alert(url);
// url=escape(url);
setTimeout(function() { deletefile(url); }, 25000);
}