Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • setTimeout() method in JavaScript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 127
    Comment on it

    The setTimeout() method in JavaScript sets a time interval during which the CPU wait to perform the task specified in it. It alter the time interval of the CPU to perform the tasks.

    The Syntax of the setTimeout() is as :

    setTimeout(task1,Timeinterval);
    

    The task1 here is the task to be performed by the Javascript and the Timeinterval defines the interval for which the CPU delays its execution.

    The example for the same is as :

    setTimeout(alert(2000), 2000);
    

    In the above code the method setTimeout() is been passed with two parameters the first one is the task to be performed and the another one is the time interval in milliseconds to which the CPU execution altered i.e., for 2000 milliseconds or 2 seconds.

    we can use the same method on the large scale in our project to implement the Image Slider as it delays the execution for some interval it delays the call for the function which performs the image change task

    The same can be done as :

    var ImgSlider = new Array("banner1.png","banner2.png","banner3.png","banner4.png");
    var Slide=0;
    
    function startTimer() {
    
           if(Slide > 3){
                  Slide = 0;
                  document.DisplaySlide.src="Images/" + ImgSlider[Slide];
                  setTimeout(startTimer, 2000); // wait 2 seconds before calling go Inactive
                  }else{
    
                  document.DisplaySlide.src="Images/" + ImgSlider[Slide];
                  Slide++;
                  setTimeout(startTimer, 2000); // wait 2 seconds before calling go Inactive
                 } 
           }    
    

    In the above code the array ImgSlider contains the array of strings of Imge file names which are used in the Slider,

    "document.DisplaySlide.src=""Images/" + ImgSlider[Slide];" in this statement the DisplaySlide is the Id of the of the Image tag in the HTML file where we are setting the src path of the image tag, ("Images/" + ImgSlider[Slide];) we have passed the array in the path which on increment the index value changes the Image file name and accordingly the file gets displayed on the Slider.

    setTimeout(startTimer, 2000); in this code the call to the startTimer() method after every 2000 milliseconds or 2 seconds gets delayed. so, as a result the Image get changed after every 2 seconds.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: