-1

Is there a way to make a POST request in Javascript/jQuery and use that variable as a setTimeout time?

$.post('file.php', { action: "gettimeout"}, 
function(time){
    return time;
});
setInterval(function(){ alert("Hello"); }, time);

is what I am trying to do.

I am currently able to do but I am doing it synchronously.

var time = $.ajax({
    type: 'POST',
    url: 'file.php',
    data: {action: "gettimeout"},
    async: false
});
return Number(time.responseText);
setInterval(function(){ alert("Hello"); }, time);
Bijan
  • 7,737
  • 18
  • 89
  • 149
  • 1
    You should use callbacks. Have a look at this answer: http://stackoverflow.com/a/14220323/1056133 – jayantS Dec 07 '16 at 06:38

1 Answers1

1

Just put your call to setInterval (or setTimeout) inside the Ajax callback function:

$.post('file.php', { action: "gettimeout"}, function(time){
    setInterval(function(){ alert("Hello"); }, time);
});

NB: there is no sense in returning a value in the Ajax callback function. See How do I return the response from an asynchronous call.

Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286