Joe
Joe

Reputation: 69

auto submit and refresh once

Pls help me to modify this one I want it to submit and refresh only once not every 10sec

<script type="text/javascript">
window.onload=function(){
    var auto = setTimeout(function(){ autoRefresh(); }, 100);

    function submitform(){

      document.forms["myForm"].submit();
    }

    function autoRefresh(){
       clearTimeout(auto);
       auto = setTimeout(function(){ submitform(); autoRefresh(); }, 10000);
    }
}
 </script>

Upvotes: 0

Views: 944

Answers (2)

Medard
Medard

Reputation: 1059

You can use local storage to check if the page is being loaded for the very first time.

$(function() {
    var canSubmit = localStorage.getItem("can_submit");
    if(!canSubmit) {
       // first time loaded!
       document.forms['myForm'].submit();
       localStorage.setItem("can_submit","1");
       window.location.reload();
    }else{
       //You can remove it if needed
        localStorage.removeItem("can_submit");
    }
});

Upvotes: 1

alizahid
alizahid

Reputation: 969

window.onload = function() {
    if (window.location.hash !== '#done') {
        document.forms['myForm'].submit()

        window.location.hash = 'done'

        window.location.reload()
    }
}

This will submit the form and reload the page only once. The hash will check the URL if it's already been submitted.

Upvotes: 3

Related Questions