javascript - Recursion with arguments method -
the script must have print 'hello', 'good bye', because of entries on function call. prints once. why? what's wrong here bellow.
pd: doesn't work. if comment recursion call line
<html> <head> </head> <body> <script type="text/javascript"> function writing(i,first,second) { len=arguments.length; if (i<=len) { current=arguments[i]; c=0; inter=setinterval(function() { if (c>=current.length) { clearinterval(inter); } else { field=document.getelementbyid('div1'); field.innerhtml+=current[c]; c+=1; } },200); } i<len?writing(i+1,first,second):writing(i=0,first,second); } writing(1,'hello','good bye'); </script> <div id="div1"></div> </body>
there many problems code , first was infinite loop (never ending) , second variable declaration , , others...
here have attached snippet , please run , check, if looking for.
i have add settimeout fullfill requirement.
var interval_counter = 0; function writing(i, first, second) { var len = arguments.length; if (i != 0 && <= len) { var current = arguments[i]; var c = 0; settimeout(function() { var inter = setinterval(function() { if (c >= current.length) { clearinterval(inter); } else { field = document.getelementbyid('div1'); field.innerhtml += current[c]; c += 1; } }, 200); }, 200 * interval_counter); interval_counter = interval_counter + current.length; < (len - 1) ? writing(i + 1, first, second) : writing(i = 0, first, second); } else { return false; } } writing(1, 'hello', 'good bye');
<div id="div1"></div>
Comments
Post a Comment