Solved

JS: Something wrong with my javascript?

Posted on 2011-09-28
6
191 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 49
100% tall div not scrollable on iPhone 3 20
ASP.NET Content Page 3 25
javascript - insert into js doc help 2 21
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

840 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