Solved

jqery function wont work

Posted on 2013-05-10
1
333 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
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

708 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

18 Experts available now in Live!

Get 1:1 Help Now