javascript - Having function as argument in setInterval -


i'm trying pass function setinterval function. doesn't seem working code. right code below works @ first, how pass function parameters when calling setinterval function?

//works here  ajaxupdateone(function(countone,counttwo) {     var itemcount = countone;     var totalcount = counttwo;     console.log(itemcount);     console.log(totalcount); });  //does not work here.  var myvar = ajaxupdateone(function(countone,counttwo) {     var itemcount = countone;     var totalcount = counttwo;     console.log(itemcount);     console.log(totalcount); });  setinterval(myvar,8000); 

while code posted partial, , not clear ajaxupdateone() does, assuming calls $.ajax() , updates page based on response results. however, able pass parameters function called later in setinterval(), following construct can used:

var deferredupdate = function(countone, counttwo) {   return function() { ajaxupdateone(countone, counttwo); } }   setinterval(deferredupdate(42,3), 8000); setinterval(deferredupdate(1,2), 2000); 

the trick here deferredupdate() returns function itself, not value. parameters resulting function bound in particular closure. 1 of basic techniques in functional programming.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -