Javascript 和 DOM 事件交互和可能的競爭條件 (Javascript and DOM event interaction and possible race conditions)


問題描述

Javascript 和 DOM 事件交互和可能的競爭條件 (Javascript and DOM event interaction and possible race conditions)

Scenario: Preloading images

  1. Perform an ajax query
  2. Show loading screen
  3. Retrieve results from ajax query
  4. Insert images into the dom
  5. Wait for images to finish loading
  6. Hide loading screen

I was thinking of doing the following:

function ajaxCallback(results) {
   /* snip insert into dom code */

   $(".waitForLoad").each(function() {
      imageLoadingCount++;
      $(this).load(imageLoaded);
   });
}

var imageLoadingCount = 0;
function imageLoaded() {
   imageLoadingCount‑‑;
   if (imageLoadingCount == 0)
      HideLoadingScreen();
}

I'm not quite sure of the interaction between the browser DOM and javascript. Does the DOM wait for the javascript to finish executing before it starts loading the images? I'm worried about possible race conditions.

‑‑‑‑‑

參考解法

方法 1:

Javascript should run in parallel with the loading, unless you're using all the connections with AJAX requests. The browser still functions normally while javascript runs, which is the whole point behind it in the first place.

方法 2:

The load event observer is a little scary because it will work only if you set it before the element has completely loaded, if you set it after that nothing will happen. Therefore, it really depends on when you call ajaxCallback(). I would recommend using onSuccess rather than onComplete.

Also, I wonder if it's possible to use the load event observer on $(".waitForLoad") itself?

方法 3:

You can safely execute ajaxCallback within $.ready() and get rid of most problems. And note that DOM is not updated for newly created tags.

Also note that ‑ The HTTP/1.1 specification suggests that browsers download no more than two components in parallel per hostname. If you serve your images from multiple hostnames, you can get more than two downloads to occur in parallel.

In many scenarios, users will get annoyed if you are showing them loading screens for more than a second or two. Waiting for images to get loaded is hardly a good idea.

(by Richard NienaberIan ElliottDetectArpit Tambi)

參考文件

  1. Javascript and DOM event interaction and possible race conditions (CC BY‑SA 3.0/4.0)

#race-condition #conditional-statements #javascript #DOM #events






相關問題

Javascript 和 DOM 事件交互和可能的競爭條件 (Javascript and DOM event interaction and possible race conditions)

防禦 System.Collections.Concurrent.ConcurrentDictionary 中的競爭條件 (Defending against race conditions in System.Collections.Concurrent.ConcurrentDictionary)

可能一次在 PHP 中多次寫入同一個文件? (Potentially write to same file in PHP multiple times at once?)

靜態線程安全 (Thread safety of static)

CancellationTokenSource.Cancel 引發 ObjectDisposedException (CancellationTokenSource.Cancel throws an ObjectDisposedException)

如何處理 Web 應用邏輯和數據庫並發? (How to handle Web application logic and database concurrency?)

Linux IRQ 處理程序中的固有競爭條件 (Inherent race condition in Linux IRQ handlers)

SQL Server 進程隊列競爭條件 (SQL Server Process Queue Race Condition)

WCF 服務僅在客戶端收到結果時才寫入日誌 (WCF service writes log only if client receives results)

將 SWT 與 JOGL 一起使用時發生隨機崩潰(競爭條件?) (Random crashes when using SWT with JOGL (race condition?))

std::shared_ptr 的線程安全 (Thread safety with std::shared_ptr)

如何更新 JSON 類型列中的特定值 (How can I update specific value from my JSON type column)







留言討論