Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11
  1. #1

    OOP - gallery.Play() is not a function

    salve ragazzi!

    Ho bisogno di voi perchè non mi capisco!

    Ho creato la seguente classa javascript:

    codice:
    function EmarGallery(id,numStart)
    {
    this.id = id;
    this.numStart = numStart;
    this.images = null;
    this.interval = null;
    this.OpacityIn = 0;
    this.IntervalIn = null;
    this.OpacityOut = 1;
    this.IntervalOut = null;
    this.current = null;
    this.prec = null;
    
    this.Load = function()
    {
    gallery = document.getElementById(this.id);
    		
    	if(gallery != null)
    	{
    		this.images = new Array();
    		var node = gallery.firstChild;
    
    		while(node)
    		{
    			if (node.nodeType == 1)
    			{
    				this.images.push(node);
    			}
    			node = node.nextSibling;
    		}
    		alert("loaded!");
    
    	}
    };
    this.ChangeImg = function(arg)
    {
    	this.prec = this.current;
    
    	if(arg > (this.images.length-1)) this.current = 0;
    	else if(0 > arg) this.current = this.images.length-1;
    	else this.current = arg;
    
    	if(this.images[this.prec] != null)
    	{
    		//IntervalIn = setInterval('this.FadeOut(this.images[this.prec])',1);
    		this.images[this.prec].style.display = 'none';
    		this.setOpacity(this.images[this.prec],0);
    	}
    	this.images[this.current].style.display = 'block';
    	//this.images[this.current].style.left = Math.floor(Math.random()*300);
    	this.IntervalIn = setInterval('this.FadeIn(this.images[this.current])',1);
    };
    this.ChangeRand = function()
    {
    	var num;
    	
    	do{
    	num = Math.floor(Math.random()*images.length);
    	}while(num == this.current || num == this.prec)
    	
    
    	this.ChangeImg(num);
    };
    this.Play = function(sec,rand)
    {
    		if(rand != true)
    		{
    			if(this.numStart != null) this.ChangeImg(this.numStart);
    			else this.ChangeImg(0);
    
    			if(this.images.length != 1)
    			{
    			this.interval = setInterval('this.ChangeImg(this.current+1)',sec*1000);
    			}
    		}
    		else{
    			if(this.numStart != null) this.ChangeImg(this.numStart);
    			else this.ChangeRand();
    
    			if(this.images.length != 1)
    			{
    			this.interval = setInterval('this.ChangeRand()',sec*1000);
    			}
    		}
    };
    this.Stop = function()
    {
    	clearInterval(this.interval);
    };
    this.setOpacity = function(obj,value)
    {
    	if(obj.style.filter != null) obj.style.filter = 'alpha(opacity=' + value*100 + ')';
    	else if(obj.filters != null) obj.filters.alpha.opacity = value*100;
    	else obj.style.opacity = value;
    };
    this.FadeIn = function(obj)
    {
    	if(this.OpacityIn >= 1)
    	{
    		clearInterval(this.IntervalIn);
    		this.IntervalIn = null;
    		this.OpacityIn = 0;
    	}
    	else
    	{
    	this.OpacityIn = this.OpacityIn + 0.05;
    	this.setOpacity(obj,this.OpacityIn);
    	}
    };
    this.FadeOut = function(obj)
    {
    	if(this.OpacityOut <= 0)
    	{
    		clearInterval(this.IntervalOut);
    		this.IntervalOut = null;
    		this.OpacityOut = 1;
    	}
    	else
    	{
    	this.OpacityOut = this.OpacityOut - 0.05;
    	this.setOpacity(obj,this.OpacityOut);
    	}
    };
    };
    poi ho questo file:
    codice:
    var gallery = new EmarGallery("PortfolioGallery",0);
    
    window.onload = function()
    {
    	LoadMenu();
    	externalLinks();
    	gallery.Load();
    	gallery.Play(4,true);
    }
    
    window.onunload = function()
    {
    	gallery.Stop();
    }
    dove mi da i seguenti errori:
    • gallery.Play is not a function
    • gallery.Stop is not a function


    La cosa che mi fa incazzare è che non capisco perchè gallery.Load() funziona!


  2. #2
    Prova a usare nomi diversi, chiamali ad esempio play e stop in minuscolo.

  3. #3
    niente! non cambia! ma in ogni caso Load è con l'iniziale maiuscola!

  4. #4
    Il fatto è che Play e Stop sono due metodi nativi. In ogni caso il problema è che quando tu chiami setTimeout dai per scontato che il this si riferisca all'istanza della classe EmarGallery; al contrario quel this si riferisce a window.
    Come risolvere? E' molto banale: tutto ciò che devi fare è crearti una variabile, ad esempio this_, a cui assegni l'istanza:
    codice:
    function EmarGallery(id,numStart)
    {
    var this_ = this;
    this.id = id;
    ecc....
    Infine nel setTimeout basta che al posto di this.Play o this_.Stop scrivi this_.Play o this_.Stop

  5. #5
    grazie mille per l'attenziona ma io non ho nessun timeout! puoi indicarmi precisamente dove devo usare la nuava variabile this_?
    in che punto del codice? puoi riscrivere tutto con le correzioni?

    Te ne sarei infinitamente grato!


  6. #6
    Originariamente inviato da MRj92
    grazie mille per l'attenziona ma io non ho nessun timeout!
    E' ovvio che lo stesso discorso vale per setInterval
    codice:
    function EmarGallery(id,numStart)
    {
    var this_ = this;
    this.id = id;
    this.numStart = numStart;
    this.images = null;
    this.interval = null;
    this.OpacityIn = 0;
    this.IntervalIn = null;
    this.OpacityOut = 1;
    this.IntervalOut = null;
    this.current = null;
    this.prec = null;
    
    this.Load = function()
    {
    gallery = document.getElementById(this.id);
    		
    	if(gallery != null)
    	{
    		this.images = new Array();
    		var node = gallery.firstChild;
    
    		while(node)
    		{
    			if (node.nodeType == 1)
    			{
    				this.images.push(node);
    			}
    			node = node.nextSibling;
    		}
    		alert("loaded!");
    
    	}
    };
    this.ChangeImg = function(arg)
    {
    	this.prec = this.current;
    
    	if(arg > (this.images.length-1)) this.current = 0;
    	else if(0 > arg) this.current = this.images.length-1;
    	else this.current = arg;
    
    	if(this.images[this.prec] != null)
    	{
    		//IntervalIn = setInterval('this_.FadeOut(this.images[this.prec])',1);
    		this.images[this.prec].style.display = 'none';
    		this.setOpacity(this.images[this.prec],0);
    	}
    	this.images[this.current].style.display = 'block';
    	//this.images[this.current].style.left = Math.floor(Math.random()*300);
    	this.IntervalIn = setInterval('this_.FadeIn(this.images[this.current])',1);
    };
    this.ChangeRand = function()
    {
    	var num;
    	
    	do{
    	num = Math.floor(Math.random()*images.length);
    	}while(num == this.current || num == this.prec)
    	
    
    	this.ChangeImg(num);
    };
    this.Play = function(sec,rand)
    {
    		if(rand != true)
    		{
    			if(this.numStart != null) this.ChangeImg(this.numStart);
    			else this.ChangeImg(0);
    
    			if(this.images.length != 1)
    			{
    			this.interval = setInterval('this_.ChangeImg(this.current+1)',sec*1000);
    			}
    		}
    		else{
    			if(this.numStart != null) this.ChangeImg(this.numStart);
    			else this.ChangeRand();
    
    			if(this.images.length != 1)
    			{
    			this.interval = setInterval('this_.ChangeRand()',sec*1000);
    			}
    		}
    };
    this.Stop = function()
    {
    	clearInterval(this.interval);
    };
    this.setOpacity = function(obj,value)
    {
    	if(obj.style.filter != null) obj.style.filter = 'alpha(opacity=' + value*100 + ')';
    	else if(obj.filters != null) obj.filters.alpha.opacity = value*100;
    	else obj.style.opacity = value;
    };
    this.FadeIn = function(obj)
    {
    	if(this.OpacityIn >= 1)
    	{
    		clearInterval(this.IntervalIn);
    		this.IntervalIn = null;
    		this.OpacityIn = 0;
    	}
    	else
    	{
    	this.OpacityIn = this.OpacityIn + 0.05;
    	this.setOpacity(obj,this.OpacityIn);
    	}
    };
    this.FadeOut = function(obj)
    {
    	if(this.OpacityOut <= 0)
    	{
    		clearInterval(this.IntervalOut);
    		this.IntervalOut = null;
    		this.OpacityOut = 1;
    	}
    	else
    	{
    	this.OpacityOut = this.OpacityOut - 0.05;
    	this.setOpacity(obj,this.OpacityOut);
    	}
    };
    };
    Visto che non ho testato potrebbero anche esserci altri errori che non ho notato.

  7. #7
    non cambia niente! non "trova" la funzione Gallery.Play()!

    aiuto!

  8. #8
    Prova così allora:
    codice:
    function EmarGallery(id,numStart)
    {
    var this_ = this;
    this.id = id;
    this.numStart = numStart;
    this.images = null;
    this.interval = null;
    this.OpacityIn = 0;
    this.IntervalIn = null;
    this.OpacityOut = 1;
    this.IntervalOut = null;
    this.current = null;
    this.prec = null;
    
    this.Load = function()
    {
    gallery = document.getElementById(this.id);
    		
    	if(gallery != null)
    	{
    		this.images = new Array();
    		var node = gallery.firstChild;
    
    		while(node)
    		{
    			if (node.nodeType == 1)
    			{
    				this.images.push(node);
    			}
    			node = node.nextSibling;
    		}
    		alert("loaded!");
    
    	}
    };
    this.ChangeImg = function(arg)
    {
    	this.prec = this.current;
    
    	if(arg > (this.images.length-1)) this.current = 0;
    	else if(0 > arg) this.current = this.images.length-1;
    	else this.current = arg;
    
    	if(this.images[this.prec] != null)
    	{
    		//IntervalIn = setInterval(function() { this_.FadeOut(this_.images[this_.prec]); },1);
    		this.images[this.prec].style.display = 'none';
    		this.setOpacity(this.images[this.prec],0);
    	}
    	this.images[this.current].style.display = 'block';
    	//this.images[this.current].style.left = Math.floor(Math.random()*300);
    	this.IntervalIn = setInterval(function() { this_.FadeIn(this_.images[this_.current]); },1);
    };
    this.ChangeRand = function()
    {
    	var num;
    	
    	do{
    	num = Math.floor(Math.random()*images.length);
    	}while(num == this.current || num == this.prec)
    	
    
    	this.ChangeImg(num);
    };
    this.Play = function(sec,rand)
    {
    		if(rand != true)
    		{
    			if(this.numStart != null) this.ChangeImg(this.numStart);
    			else this.ChangeImg(0);
    
    			if(this.images.length != 1)
    			{
    			this.interval = setInterval(function() { this_.ChangeImg(this_.current+1); },sec*1000);
    			}
    		}
    		else{
    			if(this.numStart != null) this.ChangeImg(this.numStart);
    			else this.ChangeRand();
    
    			if(this.images.length != 1)
    			{
    			this.interval = setInterval(function() { this_.ChangeRand(); },sec*1000);
    			}
    		}
    };
    this.Stop = function()
    {
    	clearInterval(this.interval);
    };
    this.setOpacity = function(obj,value)
    {
    	if(obj.style.filter != null) obj.style.filter = 'alpha(opacity=' + value*100 + ')';
    	else if(obj.filters != null) obj.filters.alpha.opacity = value*100;
    	else obj.style.opacity = value;
    };
    this.FadeIn = function(obj)
    {
    	if(this.OpacityIn >= 1)
    	{
    		clearInterval(this.IntervalIn);
    		this.IntervalIn = null;
    		this.OpacityIn = 0;
    	}
    	else
    	{
    	this.OpacityIn = this.OpacityIn + 0.05;
    	this.setOpacity(obj,this.OpacityIn);
    	}
    };
    this.FadeOut = function(obj)
    {
    	if(this.OpacityOut <= 0)
    	{
    		clearInterval(this.IntervalOut);
    		this.IntervalOut = null;
    		this.OpacityOut = 1;
    	}
    	else
    	{
    	this.OpacityOut = this.OpacityOut - 0.05;
    	this.setOpacity(obj,this.OpacityOut);
    	}
    };
    };

  9. #9
    niente! ma non penso che sia quello il problema ma la dichiarazione della classe!
    l'interprete dice che Play non è una funzione! c'è qualcosa di sbagliato su come creo la classe con le varie funzioni?

  10. #10
    Boh, io ho fatto delle prove e non mi da nessun errore del genere...
    Mi sa che dovresti uploadare un esempio.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.