0

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">
1
  • 1
    You are missing a ' after data='id=6. Commented Feb 4, 2010 at 10:30

3 Answers 3

2

This will trigger a button click every second:

window.setInterval(function() {
    $('#btnStart').trigger('click');
}, 1000);
Sign up to request clarification or add additional context in comments.

2 Comments

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...
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.
1
window.setTimeout(function(){$('#btnStart').click();}, 60000);

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.