/    Sign up×
Community /Pin to ProfileBookmark

Help with Javascript

Hello.

this is my js:

[CODE] <script language=”javascript” type=”text/javascript”>
var pics = new Array([1,7,’Summer Beckett prays with a new convert’], [2,7,’Bethany Airgood is whole-heartily seeking the LORD’], [3,7,’Sarah Fuller is seeking the LORD’], [4,7,’Arew McGrew on his face before GOD.’], [8,9,’Smash and Bash Flyer’]);
function cp() {
var rand_no = Math.ceil(5*Math.random());
document.getElementById(‘ipip’).innerHTML = “<img src=’http://abirocks.com/imgs/adminUpload/” + pics[rand_no][0] + “.JPG’ height=’200′ /><br />” + pics[rand_no][2];
alert(rand_no);
if (rand_no == last_no){
cp();
}else{
setTimeout (‘cp()’, 5000);
var last_no = rand_no;
}
}

cp();
</script>
[/CODE]

I have images named 1.JPG, 2.JPG, 3.JPG, etc… This code is in my header. In my body I have a div with an id of ipip. You can view the page live if you need anymore information at [URL=”http://abirocks.com”]http://abirocks.com[/URL]. I am trying to rotate pictures every 5 seconds and have a picture autoload when the page first loads.

thanks for your help

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@YourChildSep 25.2008 — you should declare and initialize last_no outside the function like this:

var last_no = "";

function cp(){

....


if you did declare last_no outside the function, and you are declaring


var last_no again inside the function, like this:

[COLOR="Red"]var last_no = "";[/COLOR]

function cp(){

....

if (rand_no == last_no){

cp();

}else{

setTimeout ('cp()', 5000);

[COLOR="Red"]var last_no [/COLOR]= rand_no;

}

[/code]

last_no outside and inside the function are two different variables and thus, the if statement will always be false.

Hope that fixes the problem. If not, tell us what the problem is with the code.

By the way, I'm glad to see you building a site for the Lord. Praise God! Jesus needs more programmers!!!!
Copy linkTweet thisAlerts:
@ucstreamingauthorSep 25.2008 — I believe I declared the var last_no correctly now, but I am still not getting anything (both in FF3 and IE7).

thanks

dave

PS: Just to get a non-church member option, what/who do you think this website is for?
Copy linkTweet thisAlerts:
@Mr_JSep 25.2008 — Give it a try this way

[CODE]<script type="text/javascript">

var pics = new Array(
[1,7,'Summer Beckett prays with a new convert'],
[2,7,'Bethany Airgood is whole-heartily seeking the LORD'],
[3,7,'Sarah Fuller is seeking the LORD'],
[4,7,'Arew McGrew on his face before GOD.'],
[8,9,'Smash and Bash Flyer']
);

onload = function cp(){

var rand_no = Math.floor(5*Math.random());

//alert(rand_no);

if(rand_no == last_no){
cp();
}
else{
setTimeout ('cp()', 5000);
}

document.getElementById('ipip').innerHTML = "<img src='http://abirocks.com/imgs/adminUpload/" + pics[rand_no][0] + ".JPG' height='200' /><br />" +pics[rand_no][2];

last_no = rand_no;

}

</script>[/CODE]
Copy linkTweet thisAlerts:
@ucstreamingauthorSep 25.2008 — thanks for the quick reply, put I still got nothing.
Copy linkTweet thisAlerts:
@YourChildSep 26.2008 — wait a minute..I just noticed...you are calling cp() at the top of your file correct? If this is so, your html has not loaded yet. Therefore, document.getElementById('ipip') will return null. you need to call cp() after the document has been loaded to your browser. you can do something like:

<i>
</i>&lt;script&gt;

addOnload(cp);

function addOnloadEvent(func){
var oldonload = window.onload;
if(typeof window.onload != 'function'){
window.onload = func;
}else{
window.onload = function(){
oldonload();
func();
}
}
}


var pics = new Array(
[1,7,'Summer Beckett prays with a new convert'] ...

&lt;/script&gt;



I just browsed the page again it looks like you got it working?

From what I see, it looks like the site is about your church or about a Bible school?
Copy linkTweet thisAlerts:
@ucstreamingauthorSep 26.2008 — Correct. I did get it working, but I forgot to post my new code here.


<script language="javascript" type="text/javascript">

var last_no = 0;

var last_no2 = 0;

var pics = new Array(

[1,7,'Summer Beckett prays with a friend'],

[2,7,'Bethany Airgood is whole-heartily seeking the LORD'],

[3,7,'Sarah Fuller is seeking the LORD'],

[4,7,'Arew McGrew on his face before GOD.'],

[5,11,'Rev. Frandford just off the roof'],

[8,9,'Smash and Bash Flyer'],

[10,0,'Kids helping in Grad. Drama 08'],

[11,0,'Local Family up in Duluth'],

[12,0,'Sound Booth in First Church'],

[13,0,'First Church from Sound Booth'],

[14,0,'Baseball & Football Field']);

function cp(){

var rand_no = Math.floor(11*Math.random());

var rand_no2 = Math.floor(11*
Math.random());

while (rand_no == rand_no2){

rand_no2 = Math.floor(11*Math.random());

}

if(rand_no == last_no || rand_no2 == last_no || rand_no == last_no2 || rand_no2 == last_no2){

cp();

return 0;

}else{

setTimeout ('cp()', 3500);

}

document.getElementById('ipip').innerHTML = "<a href='?page=3#" + pics[rand_no][1] + "'><img src='http://abirocks.com/imgs/adminUpload/" + pics[rand_no][0] + ".JPG' height='200' border='0' /></a><br /><b>" + pics[rand_no][2] + "</b>";

document.getElementById('ipip2').innerHTML = "<a href='?page=3#" + pics[rand_no2][1] + "'><img src='http://abirocks.com/imgs/adminUpload/" + pics[rand_no2][0] + ".JPG' height='200' border='0' /></a><br /><b>" + pics[rand_no2][2] + "</b>";

last_no = rand_no;

last_no2 = rand_no2;

}

</script>
[/QUOTE]


Then I just did <body onload="cp();">[/QUOTE] to start it up.

Yeah, it is for a bible college.
×

Success!

Help @ucstreaming spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...