Header menu

_________________________________________________________________________________
Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts

Saturday, 1 March 2014

Iteration through array with delay in jquery


What my problem is I have an array and i have to pass array element to some function with some delay between each call. Now if i iterate using loop the iteration has already been completed before calling function and i am only able to pass last value of my array to function. To deal with this i found a way using recursion such that there is delay between my each call to function.

Now in my example :
l is length of array
arr is array
k is index
tex is my function i am calling

Example:


 var k=0;
 var arr=[1,2,3,4,5,6,7,8];
 function tex(arr,k,l){
                    if(k<l){ 

                                 alert(arr[k]);
                                 setTimeout(function() { k++; tex(arr,k,l); },3000);
                               }
                }
  tex(arr,k,8);    


This is basic structure of code and it will call tex() after each 3 sec and tex will alert array elements one by one.

To understand more run below example:

Tuesday, 20 August 2013

Recursion in php

<?php

function recursion($int){
    if($int>0){
        echo $int."</br>";
        recursion($int-1);
        echo $int."</br>"; /*values from stack*/
        }
    }
   
recursion(5);

?>