Description:
Problem exists because setInterval
is added to global window
object and because window
object itself is not reloaded (using AJAX we only reload parts of DOM tree) setInterval
method continues to be called even when you close modal dialog or reload "page" container.
In short it is written here: http://www.w3schools.com/jsref/met_win_setinterval.asp
The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.
So, possible workarounds in this case will be one of these:
- use
setTimeout
instead ofsetInterval
and reload widget with newsetTimeout
call every time - use
clearInterval
when you close modal dialog window to get rid of old globally registered intervals
A lot of useful information about setInterval can be found here: https://developer.mozilla.org/en/docs/Web/API/window.setInterval
Testcase:
class page_company extends Page
{
function init()
{
parent::init();
$page = $this;
$grid = $page->add('Grid');
$model = $grid->setModel('Company');
$grid->addColumn('button','company');
if ($_GET['company']) {
$grid->js()->univ()
->frameURL('Employees', $page->api->url('../employee', array(
'company_id'=>$_GET['company'])))
->execute();
}
}
}
class page_employee extends Page
{
function init()
{
parent::init();
$page = $this;
$page->api->stickyGET('company_id');
$grid = $page->add('Grid');
$model = $grid->setModel('Employee');
if ($id = $_GET['company_id']) {
$model->addCondition('company_id', $id);
}
// NOTE:Important is that you add setTimeout JS chain to grid not page!
// That's because you reload only grid and you need to receive
// this JS chain from server on each reload to keep it reloading.
// $page is not reloading, only $grid is!
$grid->js(true)->univ()->setTimeout(
$grid->js()->reload()->_enclose()
,5000);
}
}
This way everything works fine except situation when you close one modal dialog and open new one in less that 5 seconds (setTimeout time). Problem is the same - setTimeout
also is added to global window
object and as result it still get executed after these 5 seconds even if you close your modal dialog in the mean time.
Last Edit
With newest ATK commit in Github you can now initialize periodic reloads of any View with just 1 simple chain.
So instead of something like this (which don't work):
$grid->js(true)->univ()->setTimeout(
$grid->js()->reload()->_enclose()
,5000);
now you can simply call:
// reload grid every 5 seconds
$grid->js(true)->reload(null,null,null,5000);
NOTE: you should apply this to exactly that View object which you want to reload periodically.