After the DOM element load refresh timer can not be closed more than the original problem

Asynchronous refresh the page after the original closed question multiple timers

problem

A teacher before outsourcing projects, because some need real-time map data update Echarts, so I used the polling operation, which put a different timers for different pages to regularly send ajax request the background check database and returns the data to foreground processing. But the emergence of this issue. After clicking the menu to switch content, the timer is stopped on a page did not happen. Means that we have to switch from page A to page B, but the timer A is not closed, at which point the timer B open. In fact, in addition to the current page timer, a timer background might also opened a variety of pages, there may be some good in the same kind of frequently requested.

the reason

Carefully read before the next index.jsp this page written by someone else, I thought before refreshing addition to the menu bar is content with embedded iframe (inline actually iframe original timer will shut down after being refreshed) , but found that the process used to refresh the content is in the right right div js conduct load (url), this is an update of that div, but before js still take effect.

var tb = $("#loadhtml");
tb.load(rootPath + sn[2]);

Solution

Since js still in force, but the contents of the div changes. Then I went to a timer corresponding page-specific elements, this element was found not find the (null), then that page is a load, it can turn off the timer.

timer = setInterval(function () {
	// 不在这个页面的时候就清除定时器
    if (document.getElementById("container") === null) {
        clearInterval(timer);
    } else {
    	//方法体
    }
}, 1 * 1000);

Here Insert Picture Description

Published 40 original articles · won praise 94 · views 9581

Guess you like

Origin blog.csdn.net/qq_41718454/article/details/102978731