-1

I have a situation where I need a returned value by calling the function. The scenario is following:

  • click on dom element,
  • with setTimeout() wait for another element is disappeared (the sign of the process is over),
  • afterwards fetch the value and return

The point is that I'm able to achieve the desired point with callbacks or nested functions, I can alert or assign to some global variable. But I need to return it from the function as it'll be called from C# HtmlDocument invokeScript() method.

Here is an example code:

function getValue() {
    $('#myElement').click();
    return waitFor();
}
function waitFor() {
    if($('#another').hasClass('active')) {
        setTimeout(function() {waitFor();}, 100);
    } else {
        // alert ($('#desiredValue').text());
        return $('#desiredValue').text();
    }
}
getValue();
  • Is requirement to return `$('#desiredValue').text()` from `waitFor` ? Can include `html` at Question ? – guest271314 Sep 15 '15 at 21:27
  • try to explain just a bit more, which functions is going to run in the timeout? getValue? waitFor? which value do you want to return? – Saar Sep 15 '15 at 21:30
  • You can't return a value from a function called by setTimeout. – bhspencer Sep 15 '15 at 21:32
  • @bhspencer _"You can't return a value from a function called by setTimeout."_ `jQuery.Deferred()` could return a value from `waitFor` from within `setTimeout` ? – guest271314 Sep 15 '15 at 21:33
  • @ArmanGalyan Can describe expected return values from `getValue`, `waitFor` ? – guest271314 Sep 15 '15 at 21:36
  • @guest271314 no, which instruction would you expect to be executed after you returned? You have to do all your work in the function you pass to the setTimeout. Ofcourse that function can call other functions but it cannot return a value. – bhspencer Sep 15 '15 at 23:32
  • @bhspencer http://stackoverflow.com/a/6101465/ – guest271314 Sep 16 '15 at 00:48
  • @guest271314 none of the solutions in the question you link return a value from a setTimeout. Promises are just a convenient way of handling callback functions. You can see in the example answer you link to that the callback passed to the setTImeout doesn't have a return statement, it calls dfd.resolve(); – bhspencer Sep 16 '15 at 01:12
  • @bhspencer `dfd.resolve()` can return a value from within `setTimeout` which could be returned from `timeout` with `return dfd.promise()` ? – guest271314 Sep 16 '15 at 01:29
  • I am afraid you are mistaken. dfd.resolve() does not return a value. It calls the callback that you pass to the done method on the promise. You are just chaining callbacks you are not returning values. – bhspencer Sep 16 '15 at 01:49
  • It is impossible to return something from a setTimeout as the callback is run on the next event. The event that called it must be complete by definition before the callback finishes and therefor cannot be on the stack as a return address. – bhspencer Sep 16 '15 at 01:51
  • I've edited the code, hope it does make sense :) – Arman Galyan Sep 16 '15 at 06:09

1 Answers1

0

If you're using CSS transitions (I assume you are because you are removing a class, and I assume the removal of this class causes the element to "disappear" over time), then you can listen for the transitionend event:

someElement.addEventListener('transitionend', doSomething, false);

function doSomething() {
  // make something cool happen once the animation is complete
}
Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • _"If you're using CSS transitions"_ Note, `animationend` event is different than `transitionend` event – guest271314 Sep 15 '15 at 21:32
  • @guest271314 ah yes, thank you for correcting that. I've edited my answer. – Josh Beam Sep 15 '15 at 21:33
  • I assume that invokeScript is synchronous. If so and $('#another').hasClass('active') will return false, then you will need to invoke the invokeScript multiple times. – Sagi Sep 15 '15 at 21:37