Tengo dos guiones. Uno le pregunta a mi backend sobre nuevos datos en la base de datos y si hay algo nuevo, agregue esto a div con id 'box'. En el segundo script, quiero monitorear esto y si algo cambia en este div, quiero llamar a mi función. ¿Como hacer eso?
$(document).ready(function(){ refreshTable(); }); function refreshTable(){ //loading data to div $('#messbox').load('getData.php', function(){ setTimeout(refreshTable, 1000); }); } function scroll(){ //function to call $("#messbox").scrollTop($("#messbox")[0].scrollHeight); }Puede usar un MutationObserver para escuchar los cambios en la childList del elemento:
const targetNode = document.getElementById('target'); const callback = function(mutationsList, observer) { for(const mutation of mutationsList) { if (mutation.type === 'childList') { myFunction() } } }; const observer = new MutationObserver(callback); observer.observe(targetNode, {childList: true}); function myFunction(){ console.log("Change!") } <div id="target"></div> <button onclick="target.innerHTML += '<p>Hello World!</p>'">Add something to #target</button>