javascript - Web Workers creation inside Ajax callback -
i saw that's possible ajax requests inside web worker, want ajax call via jquery (outside worker, of course), , after this, pass result of callback worker. made tests , works, want know if there's wrong (memory leaks, incompatibility, instability):
  $.ajax         ({             type: 'get',              url : 'http://192.168.0.2/json.php',             data: requestdataobj,             datatype: 'json'         }).success(function(jsonresult)         {             var jsonworker = new worker('http://localhost/webmail/responsive/js/workers.js');             jsonworker.postmessage(jsonresult);             jsonworker.onmessage = function(event)             {                 alert(event.data)             }          });   as can see, pass jsonresult worker, , post message main thread. wrong this?
the problem see you're assuming browser has support window.worker, not case.
in case blocking application feasible — computation you're expecting worker light — paste entire code of worker inside ajax callback [1].
thus
...success(function (res) {     if (window.worker) {         var worker = new worker(...);         worker.onmessage = function (e) { /* ... */ };         worker.postmessage(res);     }     else {         // write here same code worker         // supposed execute "on" res.     } });   course lose performance improvement gained 2 threads.
[1] done here @afshinm.
Comments
Post a Comment