I'm trying to do some OO JavaScript to just simply swap pictures with a fade transition, but nothing happens when the page loads.
All my images are named Joe_#.jpg, where # is a number.
Here is my imagesJS.js file:
//GLOBALS
var delay = 5000;
var opacityDelay = 100;
var pathBeginning = "images\joe\Joe_";
var pathEnding = ".jpg";
//IMAGE CLASS
function imageObj(id, start, end, initDelay) {
this.obj = document.getElementById(id);
this.cur = start;
this.start = start;
this.end = end;
this.initDelay = initDelay;
this.opacity = 1;
this.fadeIn = function() {
if(this.opacity < 1) {
this.opacity = this.opacity + 0.1;
obj.style.opacity = this.opacity;
setTimeout(this.fadeIn(), opacityDelay);
}
}
this.nextImage = function() {
if(this.cur == this.end) {
this.cur = this.start;
}
else {
this.cur++;
}
obj.src = pathBeginning + this.cur.toString() + pathEnding;
this.fadeIn();
}
this.fadeOut = function() {
if(this.opacity > 0.5) {
this.opacity = this.opacity - 0.1;
obj.style.opacity = this.opacity;
setTimeout(this.fadeOut(), opacityDelay);
}
else {
this.nextImage();
}
}
this.process = function() {
setInterval(this.fadeOut(), delay + Math.floor(Math.random()*delay));
}
}
imageObj.prototype.startProcess = function() {
setTimeout(this.process(), this.initDelay);
}
//IMAGE OBJECTS
var img1 = imageObj('img1', 1, 5, 5000);
img1.startProcess();
Here is how I include everything on the HTML page:
<head>
<!-- Links/Scripts -->
<link rel="stylesheet" type="text/css" href="style.css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="myJavascript.js" type="text/javascript"></script>
<script src="imagesJS.js" type="text/javascript"></script>
And this is the myJavascript.js file (maybe it has something to do with it):
// GLOBAL VARIABLES
var body = $('.body');
//***********//
// On Load //
//***********//
$(document).ready(function(){
//MenuItem Color Change
$('.menuItem').mouseover(function(){
$(this).css({"color":"#6699ff"})
}).mouseout(function(){
$(this).css({"color":"black"})
});
});
Should I be putting the objects in the onLoad function? I can't seem to find me problem. Thanks in Advance!
//############################################################################ UPDATE 1
Here's the code after correcting the advice from net.uk.sweet below and some scope issues from this link Using setTimeout() within a JavaScript class function
UPDATED CODE -
//GLOBALS
var delay = 5000;
var opacityDelay = 100;
var pathBeginning = "images\joe\Joe_";
var pathEnding = ".jpg";
//IMAGE CLASS
function imageObj(id, start, end, initDelay) {
this.obj = document.getElementById(id);
this.cur = start;
this.start = start;
this.end = end;
this.initDelay = initDelay;
this.opacity = 1;
this.fadeIn = function() {
if(this.opacity < 1) {
this.opacity = this.opacity + 0.1;
this.obj.style.opacity = this.opacity;
setTimeout(fadeIn, opacityDelay);
}
}
this.nextImage = function() {
if(this.cur == this.end) {
this.cur = this.start;
}
else {
this.cur++;
}
this.obj.src = pathBeginning + this.cur.toString() + pathEnding;
this.fadeIn();
}
this.fadeOut = function() {
if(this.opacity > 0.5) {
this.opacity = this.opacity - 0.1;
this.obj.style.opacity = this.opacity;
setTimeout(fadeOut, opacityDelay);
}
else {
this.nextImage();
}
}
this.process = function() {
var self = this;
self.fadeOut();
setInterval(function() {self.fadeOut();}, delay+Math.floor(Math.random()*delay));
}
}
imageObj.prototype.startProcess = function() {
var self = this;
setTimeout(function() {self.process();}, this.initDelay);
}
//IMAGE OBJECTS
var img1 = new imageObj('img1', 1, 5, 5000);
img1.startProcess();
Unfortunately, it still doesn't work.... Any ideas?
//################################################################################ SOLUTION UPDATE
I had to add the creation and calling of the startProcess function in the onLoad function on my other javascript file.
Here is the solution Code:
//GLOBALS
var delay = 5000;
var opacityDelay = 100;
var pathBeginning = "images\\joe\\Joe_";
var pathEnding = ".jpg";
//IMAGE CLASS
function imageObj(id, start, end, initDelay) {
this.obj = document.getElementById(id);
this.cur = start;
this.start = start;
this.end = end;
this.initDelay = initDelay;
this.opacity = 1;
this.fadeIn = function() {
var self = this;
if(this.opacity < 1) {
this.opacity = this.opacity + 0.1;
this.obj.style.opacity = this.opacity;
setTimeout(function() {self.fadeIn();}, opacityDelay);
}
}
this.nextImage = function() {
if(this.cur == this.end) {
this.cur = this.start;
}
else {
this.cur++;
}
this.obj.src = pathBeginning + this.cur.toString() + pathEnding;
this.fadeIn();
}
this.fadeOut = function() {
var self = this;
if(this.opacity > 0.2) {
this.opacity = this.opacity - 0.1;
this.obj.style.opacity = this.opacity;
setTimeout(function() {self.fadeOut();}, opacityDelay);
}
else {
self.nextImage();
}
}
this.process = function() {
var self = this;
self.fadeOut();
setInterval(function() {self.fadeOut();}, delay + Math.floor(Math.random()*delay));
}
}
imageObj.prototype.startProcess = function() {
var self = this;
setTimeout(function() {self.process();}, this.initDelay);
}
Thanks for all the help net.uk.sweet!!! Learned a lot about scope and chrome dev tools! Hope I can help someone else one day!