treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Javascript question

  • Hello Everybody, I'm a web designer from chicago. I'm working on some javascript code. It's jquery for a custom photo rotater that I created. It works right now but I'm looking to optimize it.

    What do you guys think? Anything I can do to clean it up?

    // JavaScript Document
    
    //global parameters for controlling the sliding.
    
    function slider(numpics, imgwidth, mydiv){
    this.mypos = mydiv;
    this.newpos = 0;
    this.imgwidth = 0;
    this.myarray = new Array;
    this.mycounter = 0;
    this.total = 0;
    this.curpos = 0;
    this.times;
    
    this.intialize = intialize;
    this.moveNext = moveNext;
    this.movePrev = movePrev;
    }
    
    /*this function intializes the postions for sliding.  It has to be called on the page to make the sliding work.
    numpics = number of divisons to slide through, 
    imgwidth =  the width of the container,
    mydiv = name of div that slides
    */
    function intialize(numpics, imgwidth, mydiv){
    while(this.mycounter < numpics){
      this.myarray[this.mycounter] = (imgwidth*this.mycounter)*-1;
      this.mycounter ++;
    } 
    this.total = numpics - 1;
    this.mypos = mydiv;
    
      }
    
    /*this function controls forward sliding*/
    function moveNext(){
      if(this.newpos < this.total)
    {this.newpos += 1;}
    else
    {this.newpos =0;  }
      this.curpos = this.myarray[this.newpos];
    $(this.mypos).stop().animate({
        left:this.curpos
        }, 800, function (){});
    
    }
    
    /*this function controls backward sliding*/
    function movePrev(){
      if(this.newpos > 0)
    {this.newpos -= 1;}
    else
    {this.newpos = this.total;  }
      this.curpos = this.myarray[this.newpos];
    $(this.mypos).stop().animate({
        left:this.curpos
        }, 800, function (){});
    }
    
  • @inkblot, Only thing i would add, have you ran timing tests for your functions?

    this.myarray = [];
  • @JohnMotylJr no I had not done that. I'll give it a shot.

    Thanks for the suggestion.