Banner rotation problems with duration

Discussion in 'JavaScript' started by myJava, Jan 18, 2009.

  1. #1
    Hello everyone! I'm new here. But i gotta say it's damn' good forum, i just can't stop readin it! Wonderful! So.. let's get to the point. I have one banner rotator script that has problems. What is all about? Each banner has a duration but when the script rotates the banner, it takes the duration of the next banner. I tried everything but it wasn't enough... So please help me. Here's the code:

    function Banner(objName){
    	this.obj = objName;
    	this.aNodes = [];
    	this.currentBanner = 0;
    	
    };
    
    
    Banner.prototype.add = function(bannerType, bannerPath, bannerDuration, height, width, hyperlink) {
    	this.aNodes[this.aNodes.length] = new Node(this.obj +"_"+ this.aNodes.length, bannerType, bannerPath, bannerDuration, height, width, hyperlink);
    };
    
    
    function Node(name, bannerType, bannerPath, bannerDuration, height, width, hyperlink) {
    	this.name = name;
    	this.bannerType = bannerType;
    	this.bannerPath = bannerPath;
    	this.bannerDuration = bannerDuration;
    	this.height = height
    	this.width = width;
    	this.hyperlink= hyperlink;
    
    };
    
    
    Banner.prototype.toString = function() {
    	var str = ""
    	for (var iCtr=0; iCtr < this.aNodes.length; iCtr++){
    		str = str + '<span name="'+this.aNodes[iCtr].name+'" '
    		str = str + 'id="'+this.aNodes[iCtr].name+'" ';
    		str = str + 'class="m_banner_hide" ';
    		str = str + 'bgcolor="#FFFCDA" ';	
    		str = str + 'align="center" ';
    		str = str + 'valign="top" >\n';
    		if (this.aNodes[iCtr].hyperlink != ""){
    			str = str + '<a href="'+this.aNodes[iCtr].hyperlink+'">';
    		}
    			
    		if ( this.aNodes[iCtr].bannerType == "FLASH" ){
    			str = str + '<OBJECT '
    			str = str + 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
    			str = str + 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" '
    			str = str + 'WIDTH="'+this.aNodes[iCtr].width+'" '
    			str = str + 'HEIGHT="'+this.aNodes[iCtr].height+'" '
    			str = str + 'id="bnr_'+this.aNodes[iCtr].name+'" '
    			str = str + 'ALIGN="" '
    			str = str + 'VIEWASTEXT>'
    			str = str + '<PARAM NAME=movie VALUE="'+ this.aNodes[iCtr].bannerPath + '">'
    			str = str + '<PARAM NAME=quality VALUE=high>'
    			str = str + '<PARAM NAME=bgcolor VALUE=#FFFCDA>'
    			str = str + '<EMBED ';
    			str = str + 'src="'+this.aNodes[iCtr].bannerPath+'" '
    			str = str + 'quality=high '
    //			str = str + 'bgcolor=#FFFCDA '
    			str = str + 'WIDTH="'+this.aNodes[iCtr].width+'" '
    			str = str + 'HEIGHT="'+this.aNodes[iCtr].height+'" '
    			str = str + 'NAME="bnr_'+this.aNodes[iCtr].name+'" '
    			str = str + 'ALIGN="center" '
    			str = str + 'TYPE="application/x-shockwave-flash" '
    			str = str + 'PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">'
    			str = str + '</EMBED>'
    			str = str + '</OBJECT>'
    		}else if ( this.aNodes[iCtr].bannerType == "IMAGE" ){
    			str = str + '<img src="'+this.aNodes[iCtr].bannerPath+'" ';
    			str = str + 'border="0" ';
    			str = str + 'height="'+this.aNodes[iCtr].height+'" ';
    			str = str + 'width="'+this.aNodes[iCtr].width+'">';
    		}
    
    		if (this.aNodes[iCtr].hyperlink != ""){
    			str = str + '</a>';
    		}
    
    		str += '</span>';
    	}
    	return str;
    };
    
    
    Banner.prototype.start = function(){
    	this.changeBanner();
    	var thisBannerObj = this.obj;
    
    	setTimeout(thisBannerObj+".start()", this.aNodes[this.currentBanner].bannerDuration * 1000);
    }
    
    
    Banner.prototype.changeBanner = function(){
    	var thisBanner;
    	var prevBanner = -1;
    	if (this.currentBanner < this.aNodes.length ){
    		thisBanner = this.currentBanner;
    		if (this.aNodes.length > 1){
    			if ( thisBanner > 0 ){
    				prevBanner = thisBanner - 1;
    			}else{
    				prevBanner = this.aNodes.length-1;
    			}
    		}
    		if (this.currentBanner < this.aNodes.length - 1){
    			this.currentBanner = this.currentBanner + 1;
    		}else{
    			this.currentBanner = 0;
    		}
    	}
    	
    
    	if (prevBanner >= 0){
    		document.getElementById(this.aNodes[prevBanner].name).className = "m_banner_hide";
    	}
    	document.getElementById(this.aNodes[thisBanner].name).className = "m_banner_show";
    
    Code (markup):
    And the html here:

    <HTML>
    <HEAD>
    <script type="text/javascript" src="banner.js"></script>
    </HEAD>
    
    <BODY BGCOLOR="#FFFFFF">
    
            <script language="javascript">
                        banner1 = new Banner('banner1');
                        
                        banner1.add("FLASH", "banners/3.swf", 3, 60, 468,"http://www.google.com");
                        banner1.add("FLASH", "banners/2.swf", 6, 60, 468,"http://www.google.com");
                        banner1.add("FLASH", "banners/1.swf", 1, 60, 468,"1.html");
                        document.write(banner1);
                        banner1.start();
            </script>
    HTML:
    Thanks in advance!
     
    myJava, Jan 18, 2009 IP
  2. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #2
    
    Banner.prototype.start = function(){
    	this.changeBanner();
    	var thisBannerObj = this.obj;
    
    	setTimeout(thisBannerObj+".start()", this.aNodes[this.currentBanner].bannerDuration * 1000);
    }
    
    Code (markup):
    "this.changeBanner()" changes the object to point to the next banner, and then it calls the new banner's start() method with the new bannerDuration.

    try this:
    
    Banner.prototype.start = function() {
      var curDuration = this.aNodes[this.currentBanner].bannerDuration * 1000;
      this.changeBanner();
      var thisBannerObj = this.obj;
      setTimeout(thisBanneRObj+".start()", curDuration);
    }
    
    Code (markup):
     
    yoavmatchulsky, Jan 18, 2009 IP
  3. myJava

    myJava Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks man! It works! :D
     
    myJava, Jan 28, 2009 IP