简体   繁体   English

setTimeout回调函数的返回值在哪里?

[英]Where do return values from setTimeout call back functions go?

I have a call back, that I call back multiple times ( recursively via setTimeOut )...there is only one condition in which I want to capture a return value, that is when the callback is done calling itself back. 我有一个回叫,我多次回叫(通过setTimeOut递归)...只有一种情况需要捕获返回值,即当回调完成回叫自己时。

However on this condition, when I return something, I don't get it where I expect to, plus I don't know where it goes at all. 但是在这种情况下,当我退货时,我无法到达预期的位置,而且我也不知道该去哪里。 There are two console.log statements marking these points...in the snippet below. 在下面的代码段中,有两个console.log语句标记了这些要点。 One where I send it...and one where I expect it. 一个寄给我...寄给我一个寄望。

        if( MC.C.movePane( pane_element, 0, 0, 0, 'begin' ) ) {
            cover_element.style.display = 'none';
            console.log('I never got it');
        }
        return o_p;
    },
    movePane: function( pane_element, start, end, index, state ) {
        if( ( state === 'begin' ) ) { // init the function
            start = parseInt( window.getComputedStyle( pane_element, null ).getPropertyValue("top"), 10 );
            end = start + 40;
            index = start;
            state = 'down';
            MC.C.movePane( pane_element, start, end, index, 'down' );
        }
        if( ( state === 'down' ) && ( index < end ) ) { // move down
            index += 1;
            pane_element.style.top = ( index ) + 'px';
            setTimeout( function( ){ MC.C.movePane( pane_element, start, end, index, state ); }, 1 );
        }
        else if( ( state === 'down' ) && index === end ) { // hold
            state = 'up';
            setTimeout( function( ){ MC.C.movePane( pane_element, start, end, index, state ); }, 2000 );
        }
        else if( ( state === 'up' ) && ( index > start ) ) { // move up
            index -= 1;
            pane_element.style.top = ( index ) + 'px';
            setTimeout( function( ){ MC.C.movePane( pane_element, start, end, index, state ); }, 1 );
        }
        else if( ( state === 'up' ) && ( index === start ) ) { // all done, return
            console.log('I just returned true');
            return true;
            // document.getElementById( 'po_but_cov' ).style.display='none';
        }
    }
};

If you're asking how to recover the return value of movePane() when it's called from setTimeout() , you can't. 如果您问如何从setTimeout()调用movePane()时返回其返回值,则不能。 setTimeout() makes no provisions for capturing and returning a value. setTimeout()没有为捕获和返回值做任何准备。 But that's OK, because by the time the callback executes, the code that called setTimeout() is no longer running -- yes? 但这没关系,因为在执行回调时,名为setTimeout()的代码不再运行-是吗?

If you want the callback to communicate that it's done doing something then -- hold onto your hat -- you're going to have to give your callback a callback of its very own. 如果您想让回调函数传达它已经完成了某件事,那么-戴上帽子-您将不得不给回调函数一个它自己的回调函数。 When the callback is though doing its time-delayed thing, it can call that callback, which will do whatever the original code would have done if it had gotten the return value. 当回调正在执行其延迟的操作时,它可以调用该回调,该回调将执行原始代码在获得返回值后将执行的任何操作。

Sorry if that makes your head hurt, but that's just how it works. 抱歉,如果这样会使您的头部受伤,那就是它的工作原理。

It might look something like (sorry if the parens and braces don't quite match up) 可能看起来像(抱歉,如果括号和括号不完全匹配)

    MC.C.movePane( pane_element, 0, 0, 0, 'begin', function() {
        cover_element.style.display = 'none';
    });
    return o_p;

    // ...
    movePane: function( pane_element, start, end, index, state, myCallback ) {
        // ...
        else if( ( state === 'up' ) && ( index === start ) ) { // all done, return
            console.log('I just returned true');
            // return true;
            myCallback();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM