The script never hits a 404 so it continues to load.
If I am looking at this correctly it won’t ever hit a 404, and it shouldn’t ever hit a 404
.
You are loading a page template, the finding and loading of which is going to be done by the main query. That page is always going to be found– ie, not 404
.
Your secondary query– the one that is loading the posts– is inside the page template. It doesn’t matter if there are posts or not, it won’t trigger a 404
. That part is long past by the time the secondary query runs.
I think this is fundamentally just a design problem.
I don’t know how your AJAX works, but what you should be doing is checking for the presence of posts returned by the query, not for a 404
returned by the page. You stop when you get 0 posts. And you should really be using the AJAX API for this as well.