我设法在页面加载时打开一个弹出窗口,但仅限于第一次打开该页面时。一旦它打开,我希望它在几秒钟后自行关闭,但我无法做到这一点。
这是我正在使用的代码:
<script type="text/javascript" language="JavaScript">
$(document).on('pageshow', function (event) {
if (sessionStorage.popUpShown != 'true') {
$('#strelica').popup('open', {positionTo: '#hitnizbor'});
sessionStorage.popUpShown = 'true';
setTimeout(function () {
$("#strelica").popup("close");
}, 3000);
}
else{
alert('already shown the popup');
}
});
</script>
最佳答案
你的例子应该可以工作,但我给你做了一个更安全的版本:http://jsfiddle.net/Gajotres/Uauar/
$(document).on('pageshow', '#index', function(){
var start = setTimeout(function () {
$('#strelica').popup('open', {positionTo: '#hitnizbor'});
clearInterval(start);
}, 0);
var stop = setTimeout(function () {
$("#strelica").popup("close");
clearInterval(stop);
}, 3000);
});
此代码可用于每个页面事件,而不仅仅是 pageshow。如果您希望它只执行一次,请使用 pageinit。
ClearInterval 是为了防止不断打开和关闭。如果您有更多问题,请随时提问。
关于javascript - JQM : popup not closing on timeout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16079450/