I have a table on a page with 25 checklist items - it's quite time consuming to change them one-by-one via inline editing. So I want to provide users with a button to update them all at once. I have the code in place, it works - but I'm hitting the 10 per second limit every time - and then the code stops.
I would really appreciate a helping hand to get some timeout or response check built into it so it works.
The code:
//Button to update all checklist items
var LAST_UPDATED_FIELD_ID = "field_46";
var es_resource_records = {};
var counter = 0;
$(document).on('knack-records-render.view_59', function(event, view, records) {
$("#view_59").append("<button id='button_mark_reviewed'style='padding: 0 10px; line-height: 22px;' class='kn-button-menu'>Check alle</button>");
es_resource_records = {};
es_resource_records = records;
$("#button_mark_reviewed").click(function() {
es_last_updated();
});
});
function es_last_updated() {
Knack.showSpinner();
var records_updated = 0;
for (var x = 0; x < es_resource_records.length; x++) {
es_update_field(es_resource_records[x]['id'], LAST_UPDATED_FIELD_ID, "True", function() {
if (++records_updated == es_resource_records.length) {
location.reload();
Knack.hideSpinner();
}
});
}
}
function es_update_field(record, field, value, callback) {
var data = {};
data[field] = value;
$.ajax({
url: "https://api.knackhq.com/v1/objects/object_6/records/"+record,
type: "PUT",
headers: {
"X-Knack-Application-Id": "My key",
"X-Knack-REST-API-Key": "My key"
},
data: data,
success: function(response) {
console.log('Checks updated!!!');
}
});
}