Skip to content

fix(js): replace synchronous AJAX call in reservation.js with async pattern #1259

@JohnVillalovos

Description

@JohnVillalovos

Analysis by Claude Code.

Problem

Web/scripts/reservation.js:862-870 uses async: false in a $.ajax() call within the getLayoutItems() function. This blocks the browser's main thread while fetching schedule layout data, freezing the entire UI until the request completes (or times out).

Synchronous XMLHttpRequest on the main thread is deprecated by all major browsers and Chrome already shows console warnings for it.

Current code

var getLayoutItems = function (scheduleId, date) {
  // ...cache check...
  $.ajax({
    url: 'schedule.php',
    dataType: 'json',
    data: { dr: 'layout', sid: scheduleId, ld: date },
    success: function (data) {
      layoutCache[weekday] = data.periods;
    },
    async: false,
  });
  return layoutCache[weekday];
};

The reason async: false is used is so the function can return the result synchronously. Two callers depend on this:

  • PopulatePeriodDropDown() (line ~899) — uses the return value to populate period <select> dropdowns
  • previousDateEndsAtMidnight() (line ~847) — calls getLayoutItems() for its side effect on layoutCache, then reads the cache

Proposed fix

Convert getLayoutItems() to return a Promise (or use a callback), and make its callers async-aware:

  1. Change getLayoutItems() to return a $.ajax() Promise (just remove async: false and return the jqXHR)
  2. Update PopulatePeriodDropDown() to use .done() / .then() to populate the dropdown after the response arrives
  3. Update previousDateEndsAtMidnight() similarly, chaining the result
  4. Keep the layoutCache optimization — when the cache hits, resolve immediately without a network call

Acceptance criteria

  • No async: false in the codebase
  • Period dropdowns still populate correctly when changing reservation dates
  • Layout cache still works (no redundant requests for the same weekday)
  • No visible UI regression on the reservation page

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions