You are viewing limited content. For full access, please sign in.

Discussion

Discussion

How to add a delay before executing a portion of Javascript?

posted on July 20, 2017

I have this code so far:

$(document).ready(function () {
  $('.rows').change(row);

function row() {
  var rownumber = $('.rows input').val();
  $('.form-del-field').trigger('click'); //Deletes all rows
  for (i=0; i < rownumber; i++){
    $('#q4').trigger('click'); //create each new row
  }
};
});

Which adds rows to my table based on the number input into a field with the css class 'rows'.

 

 

The issue I'm running into is some of the lookups are breaking when I use this. I believe this is because it executes too quickly. How can I add just a 1 second or so delay before executing this?

 

Thanks

 

0 0
replied on July 20, 2017

Forms 10.2 added lookupstarted and lookupcomplete events.  Here is a thread with some info: http://answers.laserfiche.com/questions/108471/Can-we-get-a-lookups-complete-custom-event-listener-please#121241

 

There is another post that talks about using the ajaxStop event if the new 10.2 features don't work for you.  http://answers.laserfiche.com/questions/65065/postpone-java-execution-until-a-field-is-populated-following-a-view-lookup#103420

1 0
replied on July 20, 2017

Hi David,

I would suggest using the setTimeout function for jQuery. Using it, you may add a delay time in milliseconds, so if you wanted to add a 1 second delay, it would look something like this:

setTimeout(row, 1000);

This needs adjusting so it works with your code, but it should be fairly straightforward.

1 0
replied on July 20, 2017 Show version history

Edgar,

Thanks for the help! I'm pretty new to Javascript and struggling with implementing setTimeout correctly. Any change you could assist a bit more?


Thanks

 

edit: Never mind I think I got it!

 

$(document).ready(function () {
  $('.rows').change(row);

function row() {
  setTimeout(function(){
  
  var rownumber = $('.rows input').val();
	$('.form-del-field').trigger('click'); //Deletes all rows
		for (i=0; i < rownumber; i++){
			$('#q4').trigger('click'); 
		}}, 2000); //create each new row
	}	 
});

I believe that is correct

0 0
replied on July 20, 2017

Dang so I thought that would solve the issue, but it hasn't. I have a drop down that is populated by a stored procedure lookup. Everything works fine until I enter a number in for the amount of rows I want. As soon as that happens the drop down goes blank and I'm unable to select anything... Any thoughts on this?

0 0
replied on July 20, 2017

Hey David,

 

I actually tried your code and it works fine for me. I guess at this point we would need more information regarding how your queries are working and what version of forms you're on. Also, see the links Rob posted as well, perhaps those solutions may work better for you.

0 0
replied on July 21, 2017

So I've identified the root cause of the issue, but I don't know how to solve it. If I add rows to my table, then delete the first row, then add another row, the drop down breaks. However, if I add rows to my table, then delete from the bottom up and do not delete the first row, then add rows again, the drop down works fine. The drop down is filled in by a lookup, which seems to matter. I tried it without any lookups and it worked ok. However, even if I don't enter anything into the field that performs the lookup - so choice 1, choice 2, choice 3 still show in the drop down - the issue occurs. 

 

my form is basically:

Field 1 - user enters value

Dropdown - dropdown populated based on field 1 value

Table - add rows as necessary

 

It's the strangest thing. I have no idea what is happening. But the issue is definitely if I delete the first row then try adding more back.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.