Solved

JS: Something wrong with my javascript?

Posted on 2011-09-28
6
193 Views
Last Modified: 2012-05-12
I am trying to enable two js functions on Body onload. I can run either auto1 or auto2, but I cannot get them both to run at the same time.

<body onload="auto1();auto2();">


<SCRIPT LANGUAGE="JavaScript">

//--slideshow2

NewImg2 = new Array (
"images/FAM-1.jpg",
"images/FAM-2.jpg",
"images/FAM-3.jpg",
"images/FAM-4.jpg"
);

var ImgNum2 = 0;
var ImgLength2 = NewImg2.length - 1;

//Time delay between Slides in milliseconds
var delay = 3000;

var lock = false;
var run;
function chgImg2(direction) {
if (document.images) {
ImgNum2 = ImgNum2 + direction;
if (ImgNum2 > ImgLength2) {
ImgNum2 = 0;
}
if (ImgNum2 < 0) {
ImgNum2 = ImgLength2;
}
document.slideshow2.src = NewImg2[ImgNum2];
   }
}
function auto2() {
if (lock == true) {
lock = false;
window.clearInterval(run);
}
else if (lock == false) {
lock = true;
run = setInterval("chgImg2(1)", delay);
   }
}

//--slideshow1
 
NewImg1 = new Array (
"images/FUN-1.jpg",
"images/FUN-2.jpg",
"images/FUN-3.jpg",
"images/FUN-4.jpg"
);

var ImgNum1 = 0;
var ImgLength1 = NewImg1.length - 1;

//Time delay between Slides in milliseconds
var delay = 3000;

var lock = false;
var run;
function chgImg1(direction) {
if (document.images) {
ImgNum1 = ImgNum1 + direction;
if (ImgNum1 > ImgLength1) {
ImgNum1 = 0;
}
if (ImgNum1 < 0) {
ImgNum1 = ImgLength1;
}
document.slideshow1.src = NewImg1[ImgNum1];
   }
}
function auto1() {
if (lock == true) {
lock = false;
window.clearInterval(run);

}
else if (lock == false) {
lock = true;
run = setInterval("chgImg1(1)", delay);
   }
}

</script>

</head>

<body onload="auto1();auto2();">

Open in new window

<SCRIPT LANGUAGE="JavaScript">

//--slideshow2

NewImg2 = new Array (
"images/FAM-1.jpg",
"images/FAM-2.jpg",
"images/FAM-3.jpg",
"images/FAM-4.jpg"
);

var ImgNum2 = 0;
var ImgLength2 = NewImg2.length - 1;

//Time delay between Slides in milliseconds
var delay = 3000;

var lock = false;
var run;
function chgImg2(direction) {
if (document.images) {
ImgNum2 = ImgNum2 + direction;
if (ImgNum2 > ImgLength2) {
ImgNum2 = 0;
}
if (ImgNum2 < 0) {
ImgNum2 = ImgLength2;
}
document.slideshow2.src = NewImg2[ImgNum2];
   }
}
function auto2() {
if (lock == true) {
lock = false;
window.clearInterval(run);
}
else if (lock == false) {
lock = true;
run = setInterval("chgImg2(1)", delay);
   }
}

//--slideshow1
 
NewImg1 = new Array (
"images/FUN-1.jpg",
"images/FUN-2.jpg",
"images/FUN-3.jpg",
"images/FUN-4.jpg"
);

var ImgNum1 = 0;
var ImgLength1 = NewImg1.length - 1;

//Time delay between Slides in milliseconds
var delay = 3000;

var lock = false;
var run;
function chgImg1(direction) {
if (document.images) {
ImgNum1 = ImgNum1 + direction;
if (ImgNum1 > ImgLength1) {
ImgNum1 = 0;
}
if (ImgNum1 < 0) {
ImgNum1 = ImgLength1;
}
document.slideshow1.src = NewImg1[ImgNum1];
   }
}
function auto1() {
if (lock == true) {
lock = false;
window.clearInterval(run);

}
else if (lock == false) {
lock = true;
run = setInterval("chgImg1(1)", delay);
   }
}

</script>

</head>

<body onload="auto1();auto2();">

Open in new window

0
Comment
Question by:westdh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:ienaxxx
ID: 36719996
what about having a func. that collect all the func calls you want to fire at load?

otherwise try this:
eliminate the onloqd on the html tag and write this in the header
<script> </script>
0
 
LVL 10

Expert Comment

by:ienaxxx
ID: 36720007
sry, i meant <script>
window.onload=function(){

function1();
function2();
}
</script>
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36720121
Try this :

<body onload="setTimeout(function() { auto1();},50);auto2();">
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:westdh
ID: 36720280
neither of these works..
this works only if I use only one of the functions.
either auto1 or auto2
<body onload="setTimeout(function() { auto1();},50);auto2();">
0
 

Author Comment

by:westdh
ID: 36720416
Ok. Made some progress
this does works <body onload="auto1();auto2()">
Problem was within the script.

I alter this code in each auto1 and auto2
see auto1 & auto2.

So now the images change in sync for slideshow1 and slideshow2

so.. How would you change the script so that slideshow1 and slideshow2
change there images at different times. I plan on 4 image arrays
 and I would like to display them like rolling images
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link href="layout-basic.css" rel="stylesheet" type="text/css" />

<SCRIPT LANGUAGE="JavaScript">

//--slideshow2

NewImg2 = new Array (
"images/FAM-1.jpg",
"images/FAM-2.jpg",
"images/FAM-3.jpg",
"images/FAM-4.jpg"
);

var ImgNum2 = 0;
var ImgLength2 = NewImg2.length - 1;

//Time delay between Slides in milliseconds
var delay = 2000;

var lock2 = false;
var run;
function chgImg2(direction) {
if (document.images) {
ImgNum2 = ImgNum2 + direction;
if (ImgNum2 > ImgLength2) {
ImgNum2 = 0;
}
if (ImgNum2 < 0) {
ImgNum2 = ImgLength2;
}
document.slideshow2.src = NewImg2[ImgNum2];
   }
}

function auto2() {
if (lock2 == true) {
lock2 = false;
window.clearInterval(run);
}
else if (lock2 == false) {
lock2 = true;
run = setInterval("chgImg2(1)", delay);
   }
}

//--slideshow1
 
NewImg1 = new Array (
"images/FUN-1.jpg",
"images/FUN-2.jpg",
"images/FUN-3.jpg",
"images/FUN-4.jpg"
);

var ImgNum1 = 0;
var ImgLength1 = NewImg1.length - 1;

//Time delay between Slides in milliseconds
var delay = 2000;

var lock1 = false;
var run;
function chgImg1(direction) {
if (document.images) {
ImgNum1 = ImgNum1 + direction;
if (ImgNum1 > ImgLength1) {
ImgNum1 = 0;
}
if (ImgNum1 < 0) {
ImgNum1 = ImgLength1;
}
document.slideshow1.src = NewImg1[ImgNum1];
   }
}
function auto1() {
if (lock1 == true) {
lock1 = false;
window.clearInterval(run);
}
else if (lock1 == false) {
lock1 = true;
run = setInterval("chgImg1(1)", delay);
   }
}

</script>

</head>


<body onload="auto1();auto2()">

Open in new window

0
 

Author Closing Comment

by:westdh
ID: 36720457
Got it thanks

<body onload="setTimeout(function() { auto1();},50);auto2();">
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question