Hi,

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:
$('#js_activity_ajax_status').val('off');


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

    // Stop the timed function
    clearInterval(intervalID);

    // 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!
            alert('go...');
        });

    } else {
        // keep running...
    }
});

2 answers

danwellman 5600
0
points
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:
$('#js_activity_ajax_status').val('off').change();
Answered over 5 years ago by danwellman
0
points

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

Answered over 5 years ago by Edward Williams