I have a third-party JavaScript plugin on my site that generates reports with HTML tables, but it applies its own styling which is ugly and cumbersome to modify (every table element has a ton of classes on it that set a whole bunch of properties that I’d need to override, etc.). There’s no facility through the plugin to change this.
My site is built in Bootstrap 4, so I found what I could do is select these generated tables via jQuery, then simply strip all their custom classes and replace them with the standard Bootstrap table classes. Now they look and work exactly like all the other tables across my site.
The problem is, the plugin doesn’t expose any events or anything for me to hook into to know when a table has been generated. So I’ve resorted to just firing a function on an interval that looks for tables with these particular custom classes; if it finds any, then it strips them and swaps in the Bootstrap classes:
$(function () {
var checkForTables = setInterval(styleTables, 500);
function styleTables() {
$("table.tableWithWeirdStyling").each(function () {
$(this).attr("class", "table table-bordered table-striped");
});
}
});
But I can’t help but feel like this is bad for performance, because this function is firing every 500ms
indefinitely, whether any of these tables are present on the particular page or not (and I can’t clear the interval once it finds the tables and applies the styling, either, because the user always has the option to regenerate them, so I have to continually check).
My question is: if most pages on my site don’t even have any of these tables, is it actually bad for performance if the function isn’t even doing anything? If so, is there any other way I could possibly achieve this?