javascript, wait for something to be true then run action

前端 未结 3 726
清酒与你
清酒与你 2020-12-11 05:42

Well the title kindof says what I need. Because in Javascript timeouts asynchronous I need to know when something becomes true. I don\'t want busyloop.

Came up with:

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-11 05:56

    It's decent enough, if it's easy enough to read and it works just fine then it's generally good javascript.

    Performance-wise, it's generally better to call the function whenever whatever is set to true happens. So in whatever function that executes to make predicate() return true, you could just call action() at the end. But I'm sure that's what you would have done if you could, right?

    You could also look at using a callback, where you register a javascript function to a particular variable or function argument and when the function is run it executes whatever function was set to the callback variable.

提交回复
热议问题