Delays between JS avents

Hi,

I have no experience with Javascript but have been trying to include a delay between events.

First i obtain an populate a field with an ip address then the form is automatically submitted.

The IP address is taking too long to populate so the form is submittig without the IP address value.

var ipInputID = 'fieldxxxxxxxxx';
TB.render('component_5', function(data) {
    $.get("https://api.ipify.org", function(data) {
        $('#' + ipInputID)
            .val(data)
            .change();
    });
});
TB.render('component_5',function(data){
     data.ele.find($($('.af-form-submit')[1])).click();
 });

Not sure how to include this.

setTimeout(function(){
        },10000);
      ,3000);

@richardch372, no need to add a delay. Define the submit button and trigger the click in the API callback.

var ipInputID = 'fieldX';
TB.render('component_ID', function(data) {
        let formSubmit = $(data.ele.find('.form-submit button'));
        $.get( "https://api.ipify.org", function( data ) {
          $('#'+ipInputID)
              .val(data)
              .change();
          formSubmit.click();
        });  
});

Thank you @Chem

1 Like