views:

97

answers:

3

I would like to push data from mysql db to div every x seconds.

at window.setTimeout(update, 60000); - how to pass in the btnStart.click function into it again??? Not too sure about the syntax for passing in function call.

Here's the code for reference

$('#btnStart').click(function() {

$.ajax({

    url: "ajax.php",

    type: "POST",

    data: 'id=6',

    timeout: 15000,

    beforeSend: function(){ 

    },

    error: function(XMLHttpRequest, textStatus, errorThrown) {
        $("#userstatus").html('Timeout contacting server..');
           window.setTimeout(update, 60000);
    },

    success:  function(output) {                            
        output= jQuery.trim(output);
        $('#userstatus').html(output);

        window.setTimeout(update, 10000);
    },

    complete: function(){

    }

});

<div id="userstatus"></div>

<input type="button" id="btnStart" value="start now">
A: 
window.setTimeout(function(){update();}, 10000);

function update()
{
    $("#btnStart").trigger("click");
}

or you can wrap your ajax call inside another function and invoke that function in the button click and settimeout methods.

rahul
+2  A: 

This will trigger a button click every second:

window.setInterval(function() {
    $('#btnStart').trigger('click');
}, 1000);
David
+1 ... interval would be the most elegant way to do this!
Andreas Niedermair
Only problem is that the this should be started after the first click (ok easy to solve). But depending on `success` or `error` the time between the intervals differ...
Felix Kling
setInterval is a good idea, but if let's sa database server got error, it will still ajax call every 30 seconds. Maybe setTimeout written inside btnStart error: longer timeout, success: shorter timeout, would be better.
i need help
+1  A: 
window.setTimeout(function(){$('#btnStart').click();}, 60000);
Felix Kling