Windows.setTimeout() reducing my form speed

Feb 5, 2014 at 5:24 AM
this is my code for InfoPath postback

window.onload = function() {
window.setTimeout(getData, 1000);
}


function getData(){
getEmployee();
getCompany();
window.setTimeout(getData, 1000); //To handle IP's form postback
}

function getEmployee(){
var employee = [];
$().SPServices({
operation: "GetListItems",
listName: "employee",
CAMLViewFields: "",
CAMLQuery:"<Query><Where><Eq><FieldRef Name='Inactive0'/><Value Type='Boolean'>0</Value></Eq></Where></Query>",
async: false,
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
employee.push({label:$(this).attr("ows_Title"),value:$(this).attr("ows_ID")});
});
}
});

$("input[id$='FormControl0_V1_I1_T26']").autocomplete({
source: employee,
minLength: 2,
select: function( event, ui) {
alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.value) : "Nothing selected, input was " + this.value);
$("input[id$='FormControl0_V1_I1_T27']").focus();
$("input[id$='FormControl0_V1_I1_T26']").val(ui.item.label);
$("input[id$='FormControl0_V1_I1_T27']").val(ui.item.value);
event.preventDefault();
return false;
},
focus: function(event, ui){
event.preventDefault();
$("input[id$='FormControl0_V1_I1_T26']").val(ui.item.label);
      return false;
    }
});


function getCompany(){
var company = [];
$().SPServices({
operation: "GetListItems",
listName: "company",
CAMLViewFields: "",
CAMLQuery:"<Query><Where><Eq><FieldRef Name='Inactive'/><Value Type='Boolean'>0</Value></Eq></Where></Query>",
async: false,
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
company.push({label:$(this).attr("ows_Title"),value:$(this).attr("ows_ID")});
});
}
});

$("input[id$='FormControl0_V1_I1_T28']").autocomplete({
source: company,
minLength: 2,

select: function( event, ui) {
alert(ui.item ? ("You picked '" + ui.item.label + "' with an ID of " + ui.item.value) : "Nothing selected, input was " + this.value);
$("input[id$='FormControl0_V1_I1_T29']").focus();
$("input[id$='FormControl0_V1_I1_T28']").val(ui.item.label);
$("input[id$='FormControl0_V1_I1_T29']").val(ui.item.value);
event.preventDefault();
            return false;
    },
focus: function(event, ui){
$("input[id$='FormControl0_V1_I1_T28']").val(ui.item.label);
event.preventDefault();
      return false;
    }
});
}



in the function getEmployee() and getCompany() i am using autocomplete for the textbxox, for the code windows.setTimeout(getData, 1000), my form gets very slower for every 1 sec it is getting postback , is there anyother solution for postback to make my form speed
Coordinator
Feb 5, 2014 at 5:42 AM
You're calling getData inside getData. That means you're doing all the work at least twice. So the line:
window.setTimeout(getData, 1000); //To handle IP's form postback 
shouldn't be there.

You should also switch to using promises. You can set up the data requests immediately, and the wire up the autocompletes once the data is available.

M.
Feb 5, 2014 at 6:00 AM
I can't catch you, may i know in which stage you are specifying me to use getdata() in autocompletes
Feb 5, 2014 at 7:30 AM
I can't catch you, may i know in which stage you are specifying me to use getdata() in autocompletes, I have called the getData(); inside select function and also in focus function, my autocomplete function is working only for single time, if i type value second time the autocomplete is not working, if i call out of the autocompletes function but inside the getEmployee() or getCompany() my autocomplete is not working. May i know the solution please
Coordinator
Feb 5, 2014 at 2:36 PM
Ram:

I'm not going to write the code for you - that wouldn't serve either of us very well. You'd end up with something you couldn't support and I'd be spending time I'd never get back.

As I pointed out in my original post about this method, it's "hacky". The ids in the InfoPath form can change, etc.

I think you need to do some debugging on your end to see where the bottlenecks are. You don't need timeouts other than upon the initial page load to cover the asynchronous nature of InfoPath forms.

M.