Asynchronously delay JS until a condition is met

后端 未结 3 1214
太阳男子
太阳男子 2021-02-06 01:45

I have a class, ChatRoom, that can only render after it receives a long-running HTTP request (it could take 1 second or 30 seconds). So I need to delay rendering un

3条回答
  •  灰色年华
    2021-02-06 01:50

    Consider this:

    (function wait() {
        if ( chatroom.json ) {
            chatroom.render();
        } else {
            setTimeout( wait, 500 );
        }
    })();
    

    This will check every half second.

    Live demo: http://jsfiddle.net/kBgTx/

提交回复
热议问题