closures - is it possible to make the "value" self-increase in setTimeout() method in Javascript? -



closures - is it possible to make the "value" self-increase in setTimeout() method in Javascript? -

is possible create "value" self-increase in settimeout() method in javascript?

my code looks below code block. have 2 loops in init() method, , in inner loop have settimeout method create function "var myfun = function testfunc(year, value)" invoked in every 1 second.

but still have attribute called "value"

1.i want "value" increased every time,

2.or create alter "value", utilize new "value" when next time function "var myfun = function testfunc(year, value)" been invoked;

but found in function "var myfun = function testfunc(year, value)" can year correctly dataset[state], "value" same.

is there way create "value" increased or changed every time? appreciate help or answers.

... init(); function init(){ ... (var state in dataset) { var i=0; var value = 18; (var year in dataset[state]) { i++; settimeout( "myfun(" + year + "," + value + ")", i*1000); } } } var myfun = function testfunc(year, value){ // want "value" increased every time, // or create alter "value", utilize new "value" // in next time invoke function; value++; console.log("year:" + year + ",value:"+value); }

i think it's going possible if utilize settimeout function callback (which should anyway, since string notation not recommended):

function init() { // ... (var state in dataset) { var = 0; var value = 18; (var year in dataset[state]) { i++; settimeout(function() { myfun(year, ++value); }, * 1000); } } } var myfun = function testfunc(year, value) { console.log("year:" + year + ",value:" + value); }

javascript closures settimeout

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -