最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reload when setTimeout function finishes - Stack Overflow

programmeradmin2浏览0评论

I want to reload window after a this custom function finishes:

<script type="text/javascript">
$(document).ready(function(){
    setTimeout(function () {
        $('#order_form').dolPopupHide({});
    }, 3000);
    //window.location.reload();
});
</script>

Is there a way I can add the reload to the setTimeout function so it doesn't run until the timeout is over?

I want to reload window after a this custom function finishes:

<script type="text/javascript">
$(document).ready(function(){
    setTimeout(function () {
        $('#order_form').dolPopupHide({});
    }, 3000);
    //window.location.reload();
});
</script>

Is there a way I can add the reload to the setTimeout function so it doesn't run until the timeout is over?

Share Improve this question edited Oct 20, 2010 at 14:50 tmartin314 asked Oct 20, 2010 at 14:45 tmartin314tmartin314 4,18110 gold badges42 silver badges60 bronze badges 3
  • Did you try window.location.reload();? If so, how does that not meet your need? – LarsH Commented Oct 20, 2010 at 14:48
  • It reload right away basically. And the page doesn't fully load before it starts. – tmartin314 Commented Oct 20, 2010 at 14:49
  • In this context, using document.ready is overkill. setTimeout() already establishes that the code will run later. – jimbo Commented Oct 20, 2010 at 15:28
Add a ment  | 

1 Answer 1

Reset to default 7

reload needs to be inside your function:

$(document).ready(function(){
    setTimeout(function () {
        $('#order_form').dolPopupHide({});
        window.location.reload();
    }, 3000);

});

If you want to conceptually separate the work from the reload, you can do something like this:

$(document).ready(function(){
    setTimeout(function () {
        doWork();
        window.location.reload();
    }, 3000);

    function doWork() {
        $('#order_form').dolPopupHide({});
    }

});

Or, to be even more general:

function reloadAfterExec(fn) 
    return function() {
        fn();
        window.location.reload();
    }
}

$(document).ready(function(){
    setTimeout( reloadAfterExec(function() {
        $('#order_form').dolPopupHide({});
    }), 3000);

});
发布评论

评论列表(0)

  1. 暂无评论