Solved

jqery function wont work

Posted on 2013-05-10
1
343 Views
Last Modified: 2013-05-10
Hi, Why cant the following jquery work when I place the code in a separate function as below

I get no error but no animation.

$(document).ready(function(){
 
  var $val1=6;
  var $val2=10.3;
  var img = ["images/bcave1.png", "images/bcave2.png"];
  var indexNum=0;
  
	setInterval("animatepic1()",100);
 /*  var gameLoop = setInterval( function(){
	  $("#pic").attr("src", img[indexNum]);
      indexNum++;	
      if (indexNum >= 2) {indexNum = 0;} 
	   
	   } , 100);
	   */
  
 
   
 }); 
 
   function animatepic1(){
			 $("#pic").attr("src", img[indexNum]);
     		 indexNum++;	
      		 if (indexNum >= 2) {indexNum = 0;} 
	   
	    }

.....
<img id ="pic" src="images/bcave1.png" alt="eight ball" />

Open in new window

0
Comment
Question by:jagguy
[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
1 Comment
 
LVL 4

Accepted Solution

by:
Ersoy Hasan earned 500 total points
ID: 39155181
I suppose it's because indexNum and img array are defined in $(document).ready function's scope.
just change:
var indexNum=0;
//to 
window.indexNum=0;//this way you will define them in global object and they will be accessible everywhere

Open in new window

or even better move your function into ready scope and use the correct way to address a function with setInterval
$(document).ready(function(){
 
  var $val1=6;
  var $val2=10.3;
  var img = ["images/bcave1.png", "images/bcave2.png"];
  var indexNum=0;
  

  
    function animatepic1(){
			 $("#pic").attr("src", img[indexNum]);
     		 indexNum++;	
      		 if (indexNum >= 2) {indexNum = 0;} 
	   
	    }
	setInterval(animatepic1 ,100);   
 }); 
 
.....
<img id ="pic" src="images/bcave1.png" alt="eight ball" />

Open in new window

0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article discusses four methods for overlaying images in a container on a web page
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

734 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