Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why settimeout not delay the function execution?

function tryToDownload(url)
{

       oIFrm = document.getElementById('myIFrm');
       oIFrm.src = url;
      // alert(url);
      // url=escape(url);

      setTimeout(deletefile(url), 25000); 
}

following is deletfile function

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?

like image 523
User 1531343 Avatar asked Dec 22 '12 09:12

User 1531343


People also ask

Does setTimeout stop execution?

setTimeout() is an asynchronous function, meaning that the timer function will not pause execution of other functions in the functions stack.

Does setTimeout execute immediately?

Next, you can pass the milliseconds parameter, which will be the amount of time JavaScript will wait before executing the code. If you omit the second parameter, then setTimeout() will immediately execute the passed function without waiting at all.

Why is setTimeout executed at last?

setTimeout schedules the function for execution. That scheduler doesn't do its thing until after the current thread yields control back to the browser, e.g. after the last logging statement has executed.

Why are promises executed before setTimeout?

The reason the promise is executing before your timeout is that the promise isn't actually waiting for anything so it resolved right away. @frankies That has more to do with the way Promises are queued and resolved. The focus of my answer is the difference between setTimeout and Promise .


2 Answers

In this line you are calling your function and pass its result to 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:

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!

Note that according to MDN this way of passing parameters wont work in IE before IE9.

like image 119
Sirko Avatar answered Oct 07 '22 20:10

Sirko


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:

function tryToDownload(url) {

    oIFrm = document.getElementById('myIFrm');
    oIFrm.src = url;
   // alert(url);
   // url=escape(url);

   setTimeout(function() { deletefile(url); }, 25000);

}
like image 22
Guffa Avatar answered Oct 07 '22 20:10

Guffa