Custom Button Auto Click in Javascript Issue
I have a custom button named "calc" in my project on my edit page. The button adds data into a table when clicked and updates (refreshes through a header) the values on the page. There are no issues with the custom button, or the data being viewed and recorded into the table correctly. Everything is working fine.
GOAL: What I am attempting to do is auto click the button three times prior to alerting with a message that the calculation is complete using this javascript Onload event below
code:
var x = 0;
var intervalID = setInterval(function () {
// my logic here
$("#calc_5").trigger('click');
if (++x === 3) {
window.clearInterval(intervalID);
}
}, 1000);
The problem is that this code ignores the clearInterval command and keeps going forever. I have tried moving the code to the server and client areas of the custom button, but nothing changes and it still loops endlessly. I have also tired removing the redirect in the after page updated. My thinking is that it kept looping somehow. I have also tried removing this command from the javascript $("#calc_5").trigger('click'); and replacing it with an alert, or other chnage command. When I do this the code works perfectly. It's the $("#calc_5").trigger('click'); that isn't working and runs forever. I have also looked at hundreds of examples online regarding this and tried different variations to stop the setInterval, like switching to setTimeout etc. all which don't work.
Any help would be greatly appreciated with this issue.
Thank you,
The button uses this code in the client before:
pageObj.on('beforeSave', function(formObj, fieldControlsArr, pageObj){
formObj.baseParams['LinkType'] = "2";
});
$("#saveButton1").click();
The LinkType of 2 is an After Record Updated Event, that adds the edit page form fields into the database
The javascript Onload event code;
var x = 0;
var intervalID = setInterval(function () {
// my logic here
$("#calc_5").trigger('click');
if (++x === 3) {
window.clearInterval(intervalID);
}
}, 1000);