javascript - Preventing a async load function from being called repetitively in a click event -



javascript - Preventing a async load function from being called repetitively in a click event -

i have callback function loads page of info @ time. function gets called/triggered when , next links clicked. run issues animation , load completing in time when end user fast, subsequent clicks of next link , repetitively calls load function. realize async call, there way queue or pause loads/animations 1 load not running before other 1 finished?

function nextpagenav_click() { var curpage = parseint($('#curpage').attr('value').tostring()) + 1; $('#curpage').attr('value', curpage); loadpage(curpage, 'next'); } function loadpage(curpage, state) { $.ajax({ type: "post", url: "defaulttmp3.aspx/getlatestprofiles", cache: true, async: false, data: "{'pageindex': " + curpage + "}", contenttype: "application/json; charset=utf-8", datatype: "json", success: function (msg) { switch (state) { case 'previous': $('<div id="nextitems"></div>').insertbefore('#curitems'); $('#nextitems').empty(); $('#nextitems').settemplateurl('page/public/templates/profiletemplate.htm'); $('#nextitems').processtemplate(msg); $('#items').attr('style', 'left:-920px'); $('#items').animate({ "left": "+=920px" }, 500, function () { $('#curitems').remove(); $('#nextitems').attr('id', 'curitems'); } ); break; case 'next': $('<div id="nextitems"></div>').insertafter('#curitems'); $('#nextitems').empty(); $('#nextitems').settemplateurl('page/public/templates/profiletemplate.htm'); $('#nextitems').processtemplate(msg); $('#items').animate({ "left": "-=920px" }, 500, function () { $('#curitems').remove(); $('#nextitems').attr('id', 'curitems'); $('#items').attr('style', 'left:0'); } ); break; default: $('#curitems').empty(); $('#curitems').settemplateurl('page/public/templates/profiletemplate.htm'); $('#curitems').processtemplate(msg); break; } var rowsreturned = parseint(msg.d.rowsreturned.tostring()); var pagesize = parseint(msg.d.pagesize.tostring()); initprevnextlinks(rowsreturned, pagesize, curpage); }, error: function (request, status, error) { alert(request.statustext); } }); }

rather, seek throttling click

var throttleasync(callback, time) { var timeoutid = null; homecoming function() { cleartimeout(timeoutid); timoeoutid = settimeout(callback, time || 1); }; }

edit

the previous resets per call, utilize first click & ignore subsequent clicks

var throttleasync(callback, time) { var timeoutid = null; homecoming function() { if(timeoutid == null) { timoeoutid = settimeout(function() { timeoutid = null; callback; }, time || 1); } }; }

javascript jquery json

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -