JavaScript sleep() without freeze in Firefox

  • warning: getimagesize() [function.getimagesize]: URL file-access is disabled in the server configuration in /var/www/vhosts/dunkelfuerst.com/httpdocs/sites/all/themes/dunkla/page.tpl.php on line 12.
  • warning: getimagesize(http://www.hazzardofdarkness.net/images/banners/hodbanner2008-2.jpg) [function.getimagesize]: failed to open stream: no suitable wrapper could be found in /var/www/vhosts/dunkelfuerst.com/httpdocs/sites/all/themes/dunkla/page.tpl.php on line 12.
  1. //found@http://www.daniweb.com/forums/post742619.html#post742619
  2.  
  3. /**
  4.  * Netscape compatible WaitForDelay function.
  5.  * You can use it as an alternative to Thread.Sleep() in any major programming language
  6.  * that support it while <strong class="highlight">JavaScript</strong> it self doesn't have any built-in function to do such a thing.
  7.  * parameters:
  8.  * (Number) delay in millisecond
  9.  */
  10. function nsWaitForDelay(delay) {
  11. /**
  12. * Just uncomment this code if you're building an extention for Firefox.
  13. * Since FF3, we'll have to ask for user permission to execute XPCOM objects.
  14. */
  15. netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  16.  
  17. // Get the current thread.
  18. var thread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).currentThread;
  19.  
  20. // Create an inner property to be used later as a notifier.
  21. this.delayed = true;
  22.  
  23. /*
  24. * Call <strong class="highlight">JavaScript</strong> setTimeout function
  25. * to execute this.delayed = false after it finish.
  26. */
  27. setTimeout("this.delayed = false;", delay);
  28.  
  29. /**
  30. * Keep looping until this.delayed = false
  31. */
  32. while (this.delayed) {
  33. /**
  34. * This code will not freeze your browser as it's documented in here:
  35. * https://developer.mozilla.org/en/Code_snippets/Threads#Waiting_for_a_background_task_to_complete
  36. */
  37. thread.processNextEvent(true);
  38. }
  39. }
  40.  
  41. //i use it for ff-extension-.synchronous http-calls, which would freeze, but not with this workaround
  42. function makeRequest(url) {
  43. netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  44. // Get the current thread.
  45. var thread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).currentThread;
  46. // Create an inner property to be used later as a notifier.
  47. this.delayed = true;
  48.  
  49. var retHtml = '';
  50. $mb = jQuery.noConflict();
  51. $mb.ajax({
  52. url: url,
  53. async: true,
  54. dataType: "html",
  55. success: function (data) {
  56. retHtml = data;
  57. delayed = false;
  58. }
  59. });
  60. while (this.delayed) {
  61. /**
  62. * This code will not freeze your browser as it's documented in here:
  63. * https://developer.mozilla.org/en/Code_snippets/Threads#Waiting_for_a_background_task_to_complete
  64. */
  65. thread.processNextEvent(true);
  66. }
  67. return retHtml;
  68. }