Solved

JS: Something wrong with my javascript?

Posted on 2011-09-28
6
188 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
  • 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now