Mirakurun Mirakurun - 5 months ago 20
jQuery Question

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>

Answer

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