TeaNyan
TeaNyan

Reputation: 5079

Javascript pressing the button in an interval

I have a submit button, which sends the value with POST method to the same page.

<button id="log" class="btn bg-purple" formmethod=post value="2" name="start" formaction=start_server.php>Get Log</button>

When I press that button, I get the log from the file in a list using PHP. Screenshot

What I want to do now is to make a script with javascript to automatically press the "Get Log" button to reload the log file every 2 seconds so I can have an interactive log, without pressing the button myself.

I have tried this script, but only the alert is working I guess, since it doesn't reload the log file at all.

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

    function submitform(){
      alert('test');
      document.button["log"].submit();
  }

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

Upvotes: 1

Views: 273

Answers (2)

Louys Patrice Bessette
Louys Patrice Bessette

Reputation: 33933

Replace your script with this:

$(document).ready(function(){
    // this handles the click on yout "Get log" button
    $("#log").click(function(){
        //alert('test');
        console.log("Click function");
        $("#log").submit();
    });


    // This sets an interval to simulate a click every 2 seconds.
    auto = setInterval(function(){
        console.log("Interval iteration");
        $("#log").click(); 
    }, 2000);
});

Upvotes: 1

Rahul Vartak
Rahul Vartak

Reputation: 95

Give an ID to your form and replace this line -

document.button["log"].submit();

With this -

document.getElementById('formID').submit();

Upvotes: 0

Related Questions