0

My idea is to build a program that is completely timed and runs on it own, without any input from the user. However I want to use if-statements in the form of:

if(height()>10){ do something }

height() itself is a function and does something as well. call() is just another function that has to be invoked but is also timed with setTimeout. I want the return to be invoked after everything in call() is executed. However

function () height() {call(); setTimeout(function() { return client._lastAltitude;},1000);}

does not return the proper value or does not work the way I thought it would. Is it possible to time a return statement so that the if-statement is halted?

--Trying to clarify it now and giving my main code: I do this because I want to give a drone a flypath. I use setTimeout calls to send the move orders and certain timepoints. Because of this I also want to use if-statements that get called at a certain time. However as Javascript is asynchron the usual behaviour would be to instantly run the if-statement as it waits for time to pass to invoke the other functions. But the functions might alter the result of the if-statement. Because of this I want the return of height() to be returned after all the functions are invoked and done. Main code looks like this right now:

function altitude(){
    fullFlight.push("Height");
    runList.forEach(function (aufruf){
        aufruf(); });
        client.after(1, function () {
            emptyLists();
            });
function height() {
        altitude();
    client.after(2, function() { return client._lastAltitude});
}

But the return statement of height() does not work as intended. after() is like setTimeout just with incrementing offset.

Flo_ish
  • 9
  • 2
  • Welcome to stackoverflow. Post a full working example so we can debug it easier and have a read here https://stackoverflow.com/help/how-to-ask so your questions ca be answered in the best way – Huangism Dec 12 '19 at 18:04
  • _"Is it possible to time a return statement so that the if-statement is halted?"_ Yes it's possible. There are so many answers to that question that it is also **impossible** to answer it, in a generic form as you have asked it. You need to research, Promises, async functions, what setTimeout actually does, and more... but those will get you started and give you an understanding of why your question can't be answered in it's current form. – gforce301 Dec 12 '19 at 19:36
  • https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – epascarello Dec 12 '19 at 19:43
  • Thanks for the hints. However I did not find a solution that fits my needs. Surely Promises are made for this, but I can't give up on the if-else structure. In the end I settled with not solving the problem and taking it out of my code. – Flo_ish Dec 20 '19 at 23:57

1 Answers1

-1

You can do this without setTimeout()

function call() {
  console.log('call called');
}

function height() {
  let start = new Date().getTime();
  call();
  while (new Date().getTime() < start + 1000) {}
  return client._lastAltitude;
}

if ( height() > 10 ) {
  console.log('done');
}

Edit: Note that this will halt your program until height is finished executing. If that's not what you want, you could look at promises as gforce301 suggested.

Tomer Singal
  • 1
  • 1
  • 1
  • Although I understand what you try to do there, this doesnt use the timeout feature i have to include. As in asynchronous code this would start and not wait for the rest of invokes and that certainly is a problem i have to overcome. – Flo_ish Dec 12 '19 at 23:11