var timer; function message_timer() { clearInterval(timer); timer = setInterval(function() { $.ajax({ method: "POST", url: 'user_actions.php', data:{action: 'check_for_changes'}, success:function(change_detected) { if(change_detected) { refresh_chat_list(); refresh_messages(); } } }); }, 5000); } function refresh_messages() { $.ajax({ method: "POST", url: 'user_actions.php', data:{action: 'refresh_messages'}, success:function(html) { $("#msg_history").html(html); scroll_history(); } }); } function refresh_chat_list() { $.ajax({ method: "POST", url: 'user_actions.php', data: {action: 'refresh_chat_list'}, success:function(html) { $("#inbox_chat").html(html); set_chat_clicked(); } }); } function scroll_history() { var d = $("#msg_history"); d.scrollTop(d.prop("scrollHeight")); } function set_chat_clicked() { $(".chat_list").on('click', function(event) { event.stopPropagation(); event.stopImmediatePropagation(); message_timer(); $.ajax({ method: "POST", url: 'user_actions.php', data:{action: 'set_selected_chat', chat_id: $(this).attr('id')}, success:function(html) { refresh_chat_list(); refresh_messages(); } }); }); } $(window).on('load', scroll_history); $(window).on('load', message_timer); $("#inbox_chat").ready(set_chat_clicked);