在我的jQuery移动应用程序中,iam已将所有脚本加载到索引页(例如index.php),并且不使用任何(rel =“ external”或data-ajax =“ false”)来链接页面以保持应用程序加载与Ajax完全兼容。但是在我的应用程序中,我只想运行一个页面的脚本(例如home.php),离开该页面后,我想杀死正在使用的脚本。在home.php中,我正在使用$(document).on ('pageinit',function(){})来加载脚本。
但是我发现离开该页面后,iam在home.php中使用的脚本正在整个应用程序中运行。
如何在不使用(rel =“ external”或data-ajax =“ false”)的情况下停止通过page-init函数使用的脚本的运行?
这是我在home.php中使用的代码,
<script type="text/javascript">
$(document).on('pageinit', function(){
var auto_refresh = setInterval(
function ()
{
/* */
$("#color-bar").load("controller/file.txt");
}, 3000);
});
</script>
我想file.txt
在离开home.php后停止加载。
因此,奥马尔建议我提供一个ID而不是$(document)
。使用pagebeforeshow
或pageshow
,pagehide
然后清除间隔将解决此问题。以下代码对我有用,
var auto_refresh;
$("#light-page").on('pagebeforeshow', function(){
auto_refresh = setInterval(function () {
// code
}, 3000);
});
$('#light-page').on('pagehide', function () {
clearInterval(auto_refresh);
});
在HTML中添加ID:
<div data-role="page" id="light-page">
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句