Potential jQuery Listener Race Condition - Maybe Chrome Dev Tools Related

给你一囗甜甜゛ 提交于 2020-01-06 05:11:09

问题


Buckle in, this may be a longer ride than expected...

Note: all listeners are delegated to the 'body' and all triggered events are on the 'body' as well.

Intended On-Page-Load Flow

  1. Create selector with id #selectorA
  2. Register listener for #selectA value changed (listener1) that triggers ("aChanged")
  3. If condition X, register listener for aChanged that reloads the parent page (listener2)
  4. Register listener for aChanged that applies CSS changes to #selectorA (listener3)

Intended On-Change Flow

  1. User updates #selectA value -> $.trigger("aChanged")
  2. listener1 hears aChanged -> If condition, then reload whole page
  3. listener2 hears aChanged -> $.load() a div on page
  4. listener3 hears aChanged -> update some #selectA element's css classes (not the elements, just their css)

My intent is that if on-change-flow step 2 reloads the whole page, the other listeners can do whatever they want - or fail, I don't care because the page is being reloaded anyway. My assumption is that if the page is reloading, the other listeners can't affect the div/CSS of the now-loading/newly-loaded page.

My problem

Sometimes, but inconsistently seldom, when switching #selectA between two or more options that satisfy condition X (reload page condition), the listeners stop listening - or the aChanged event isn't fired.

When logging to the console I noticed that any time the listeners are about to fail (when they are registered prior to not hearing anything) that the order of loadPage - registerAllListeners changes, and instead of the console logs coming from the index page, they come from a VMxyz identifier (chrome's dev tool "this script isn't associated with a URL" identifier). If it's about to fail, most of the "registering listener" console logs come before chrome's "Navigated to https://..." log - all of these logs coming from the VMxyz identifier.

My Question(s)

  1. I sort of understand chrome's dev tools VMxyz identifier, but I could use help understanding why - some of the time - these console.log() statements come from the actual page and why they sometimes come from the VMxyz.
  2. I can't wrap my head around why the listeners would stop working sometimes. To me it seems that even if the page did reload mid-listener action, the page would just create the new listeners anyways.
  3. If someone has a recommendation on how I could further debug this, or fix it completely (please) I would appreciate it.

来源:https://stackoverflow.com/questions/53654516/potential-jquery-listener-race-condition-maybe-chrome-dev-tools-related

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!