ctreffe / alfred

Alfred - A library for rapid experiment development
MIT License
10 stars 1 forks source link

Make RepeatedCallback call strictly sequential #261

Closed jobrachem closed 6 months ago

jobrachem commented 6 months ago

In many cases it may be beneficial if the callback waits for a return, before starting the next callback. We can achieve this by switching from setInterval to recursive setTimeout use like this:

function makeRequest() {
    $.get('your/url', function(data) {
        // Handle your data here

        // Schedule the next request when the current one's complete
        setTimeout(makeRequest, {{ interval }} * 1000);
    });
}

// Make the initial request
makeRequest();

Also, we can leave it to the user to decide how exactly they want their callback to be handled. I think the setTimeout method should be the (new) default, but the setInterval method should still be selectable.