I have a function that updates a div on a timer, and the same function also runs when a user submits a form to save them waiting for the next timed execution. However, the two functions mustn't run at the same time, else they risk bringing back two lots of the new content. To prevent this, I am updating a hidden textfield when the timed function runs (e.g. setting it to 'on' and then to 'off'), and when the manually executed function runs, it checks the text of this textfield. If the text is 'on' then the manual function needs to wait until the text changes back to 'off' before it continues. I'm trying to do this with the code below, using jQuery's .change(), but .change() only seems to detect a change when I type in the [hidden] textfield manually, not when its content is changed with JavaScript. Is there a way to change that, or is there an alternative approach?

// The timed function ends with this:

// Manual function
$('#public_message_submit').click(function() {

    // Stop the timed function

    // Disable the submit button
    $(this).attr('disabled','disabled').animate({opacity: 0.25});

    // Check if the timed function is running
    var actvity_ajax_status = $('#js_activity_ajax_status').val();
    if (actvity_ajax_status == 'on') {

        // The timed function is running, so wait until it stops
        $('#js_activity_ajax_status').change(function() { // THIS ISN'T BEING DETECTED!

    } else {
        // keep running...

2 answers

danwellman 5600
This was chosen as the best answer

Try triggering the event manually when you update the text field, e.g.

// The timed function ends with this:
Answered about 8 years ago by danwellman

Thanks Dan, that's excellent, just what I was looking for!

Answered about 8 years ago by Edward Williams