javascript - Cancelling an event being delayed -
this question has answer here:
disclaimer : i'm not super proficient in javascript , there's small js code part in .net program.
i have 2 dropdown lists, reportlist , yearlist. first one's selected value dynamically populates second dropdown list using ajax. problem is, each time reportlist selected makes query database. there can @ least 200 entries in reportlist , when user uses mousewheel on dropdownlist, application, is, makes hundreds of query in short amount of time , crashes database. far have this
$('#reportlist').change(function () { settimeout(function () { populateyearsdropdownlist() }, 2000); }); i've played stoppropagation() , didn't work. can't test efficiently since test db hosted , maintained else.
i'd able scroll through without prompting queries amount of reports have gone through. thinking of adding small delay, each function ".change" cancelling last function call.
i think wasn't thought through beginning, want fix in small amount of time have.
i'd suggest assigning settimeout variable (with proper scope accessibility) , using cleartimeout stop timeout.
to read more settimeout , cleartimeout.
an example like:
var mytimeout; $('#reportlist').change(function () { if (mytimeout) cleartimeout(mytimeout); mytimeout = settimeout(function () { populateyearsdropdownlist() }, 2000); });
Comments
Post a Comment