How to call an asynchronous JavaScript function and block the original caller

前端 未结 5 1010
耶瑟儿~
耶瑟儿~ 2020-12-28 16:57

I have an interesting situation that my usually clever mind hasn\'t been able to come up with a solution for :) Here\'s the situation...

I have a class that has a g

5条回答
  •  余生分开走
    2020-12-28 17:27

    This is ugly, but anyway I think the question is kindof implying an ugly solution is desired...

    1. In your get function, serialize your query into a string.
    2. Open an iframe, passing (A) this serialized query and (B) a random number in querystring to this iframe
      • Your iframe has some javascript code that reads the SQL query and number from its own querystring
      • Your iframe asynchronously begins running the query.
      • When your iframe query is asynchronously finished, it sends it, along with the random number to a server of yours, say to /write.php?rand=###&reslt="blahblahblah"
      • Write.php saves this info somewhere
    3. Back in your main script, after creating and loading the iframe, you create a synchronous AJAX request to your server, say to /read.php?rand=####
    4. /read.php blocks until the written info is available, then returns it to your main page

    Alternately, to avoid sending the data over the network, you could instead have your iframe encode the result into a canvas-generated image that the browser caches (similar to the approach that Zombie cookie reportedly used). Then your blocking script would try to continually load this image over and over again (with some small network-generated delay on each request) until the cached version is available, which you could recognize via some flag that you've set to indicate it's done.

提交回复
热议问题