0

I use an ajax call to get the value of timer from the server. And I have to use that value for timeout in setTimeout() method. How to use functions instead of an actual value.

Here is my code:

if(isStopped) {
   setTimeout(stopLoading, getTimerValue());
   console.log("Stopped loading image");
}

getTimerValue() is my ajax call to the server. It works fine if I use an value like 5000 instead of the function. Please help me to solve this. Thanks in advance!

Update - Solved: There was an issue with my ajax call. It was returning undefined. After adding callback option, it works fine.

user2782405
  • 393
  • 1
  • 6
  • 20

1 Answers1

0

Please check this example.

var isStopped = true;

function getTimerValue() {
  return 5000;
}

function stopLoading() {
  console.log('ok')
}

if(isStopped) {
   setTimeout(stopLoading, getTimerValue());
   console.log("Stopped loading image");
}

As i set isStopped to true so its insert the if condition then its set the setTimeout function. but as i set it to execute it after 5000 so it will wait in the mean time it will execute the next line console.log("Stopped loading image"); and the after 5000 it prints ok Hope This will help

sayalok
  • 882
  • 3
  • 15
  • 30