简体   繁体   中英

Reload when setTimeout function finishes

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?

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);

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM