Something v basic might look like this
var ppp = 9;
var loaded = ppp;
var maxPosts = 50;
postData.append( 'action', 'loadmore' );
postData.append( 'paged', section_posts );
postData.append( 'posts_per_page', ppp );
if ( loaded < maxPosts ) { // check if we're below the max post number
const xhr = new XMLHttpRequest();
xhr.open( 'POST', ajaxurl );
xhr.addEventListener( 'readystatechange', function ( data ) {
if ( this.readyState === 4 && this.status === 200 ) {
loaded = loaded + ppp; // Add our per page to our loaded tally
section_posts++;
document.querySelector( '.box' ).innerHTML +=
data.target.responseText;
} else {
}
} );
xhr.send( postData );
} else {
alert( 'You have loaded the maximum number of posts' );
}
It’s not exact – it would still load ppp
more posts if the loaded
amount was 1 under for example. If you want to do something more you can put a secondary condition to check if you’re below the limit but would be above with the added ppp