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

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -