How do I cache jQuery selections?
- by David
I need to cache about 100 different selections for animating. The following is sample code. Is there a syntax problem in the second sample? If this isn't the way to cache selections, it's certainly the most popular on the interwebs. So, what am I missing?
note: p in the $.path.bezier(p) below is a correctly declared object passed to jQuery.path.bezier (awesome animation library, by the way)
This works
$(document).ready(function() {
animate1();
animate2();
})
function animate1() {
$('#image1').animate({ path: new $.path.bezier(p) }, 3000);
setTimeout("animate1()", 3000);
}
function animate2() {
$('#image2').animate({ path: new $.path.bezier(p) }, 3000);
setTimeout("animate2()", 3000);
}
This doesn't work
var $one = $('#image1'); //problem with syntax here??
var $two = $('#image2');
$(document).ready(function() {
animate1();
animate2();
})
function animate1() {
$one.animate({ path: new $.path.bezier(p) }, 3000);
setTimeout("animate1()", 3000);
}
function animate2() {
$two.animate({ path: new $.path.bezier(p) }, 3000);
setTimeout("animate2()", 3000);
}