最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set delay between two js functions? - Stack Overflow

programmeradmin3浏览0评论

I have following js code:

clientData.reloadTable( "CreateCSV", "/create/file" );
$("#downloadFrame").attr("src","/download/download");

In above code. first statement is creating an csv file on disk. And 2nd statement is downloading it(Using iframe to download file because of error when using AJAX request ). It is downloading file but with previous content. It means that it prompts me to download file before it finish updating that file.

How can I force my 2nd statement to not execute before 1st statement finished its work??

Thanks

I have following js code:

clientData.reloadTable( "CreateCSV", "/create/file" );
$("#downloadFrame").attr("src","/download/download");

In above code. first statement is creating an csv file on disk. And 2nd statement is downloading it(Using iframe to download file because of error when using AJAX request ). It is downloading file but with previous content. It means that it prompts me to download file before it finish updating that file.

How can I force my 2nd statement to not execute before 1st statement finished its work??

Thanks

Share Improve this question edited Jul 19, 2011 at 23:33 neeebzz 11.5k7 gold badges48 silver badges65 bronze badges asked Apr 26, 2011 at 7:21 AwanAwan 18.6k38 gold badges93 silver badges133 bronze badges 6
  • The ideal thing would be that the processing of one URL would 1) generate the report 2) write it to the HTTP response outputstream. In that way the response would wait until the report is ready. An alternative is checking when it's done periodically and download it only if it's ready. – helios Commented Apr 26, 2011 at 7:26
  • What is the code for reloadTable? As far as I can see it's not any standard jQuery plugin. – Shadow Wizard Commented Apr 26, 2011 at 7:28
  • clientData.reloadTable is calling a php function internally that creates CSV file on disk. – Awan Commented Apr 26, 2011 at 7:33
  • It's client side, can you post its code so we can help you add callback function like nEEbz suggested? – Shadow Wizard Commented Apr 26, 2011 at 8:06
  • @Shadow Wizard: Oh. Huge file using other js files as well....... – Awan Commented Apr 26, 2011 at 8:09
 |  Show 1 more ment

3 Answers 3

Reset to default 7

The best way to do something like this in Javascript is to use callback functions.

If it is possible to change the reloadTable function such that >

var callback = function () { $("#downloadFrame").attr("src", "/download/download") }

clientData.reloadTable("CreateCSV", "create/file", callback);

and then inside the reloadTable function, call the callback function once everything is done.

This is the true beauty of Javascript.


Otherwise you can also use setTimeout() if you have an idea how much time the reloadTable takes.

e.g. if it is to take 1 second. to plete, you can >

clientData.reloadTable( "CreateCSV", "create/file" );
var func = function () { $("#downloadFrame").attr("src","/download/download");}
setTimeout(func, 1000);

It doesn't sound very robust. But anyway:

function start() {
  doFirstThing();
  setTimeout('doSecondThing();', 1000); // execute the secondthing in 1000 ms
}

function doSecondThing() {
...
}
clientData.reloadTable( "CreateCSV", "/create/file" );

if it's an ajax call. call your download function from it's callback.

发布评论

评论列表(0)

  1. 暂无评论