Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
HTML















var count=0;
var set1;
function ImageChange()
{
var arr=["jpeg1.jpg","jpeg2.jpg","jpeg3.jpg","jpeg4.jpg","jpeg5.jpg","jpeg6.jpg","jpeg7.jpg",
"jpeg8.jpg","jpeg9.jpg","jpeg10.jpg","jpeg11.jpg","jpeg12.jpg","jpeg13.jpg","jpeg14.jpg",
"jpeg15.jpg","png1.png","png2.png"];
var v1=document.getElementById("i1");
v1.src=arr[count];
count=count+1;
if(count>=arr.length)
{
count=0;
}
};
function f1()
{
var set1=setInterval(ImageChange,1000);
}
function f2()
{
clearInterval(set1);
}



What I have tried:


















var count=0;
var set1;
function ImageChange()
{
var arr=["jpeg1.jpg","jpeg2.jpg","jpeg3.jpg","jpeg4.jpg","jpeg5.jpg","jpeg6.jpg","jpeg7.jpg",
"jpeg8.jpg","jpeg9.jpg","jpeg10.jpg","jpeg11.jpg","jpeg12.jpg","jpeg13.jpg","jpeg14.jpg",
"jpeg15.jpg","png1.png","png2.png"];
var v1=document.getElementById("i1");
v1.src=arr[count];
count=count+1;
if(count>=arr.length)
{
count=0;
}
};
function f1()
{
var set1=setInterval(ImageChange,1000);
}
function f2()
{
clearInterval(set1);
};
Posted
Updated 8-Jan-17 4:14am

1 solution

The culprit is caught in
function f1()
{
var set1=setInterval(ImageChange,1000);
}

By declaring var in a function, you are making this set1 variable local to that function as a result it is not accessible outside of that function. In fact, you already have a global set1 on top. So the solution is simple, just remove the
var
Learn more about JavaScript Scope[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900