In this scenario you can use .bind()
and .trigger()
$(function() {
setInterval(function() {
$('#slider').trigger('slide'); //triggers 'slide'
},2000); //Performs sliding every 2 seconds
});
$('#slider').bind('slide', function() {
//write your slide methods
});
Edit : Code & Demo Page : http://jsbin.com/efoje4
HTML :
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>Hello world !!</title>
<style type="text/css">
div {margin:0px auto;padding:0px; width:300px;height:200px; overflow:hidden; }
ul { margin :0px; padding:0px; list-style:none; width:1200px;height:200px;}
li{float : left;margin:0px padding:0px; width:300px;height:200px; }
img { margin:0px padding:0px; width:300px;height:200px; }
</style>
</head>
<body>
<div>
<ul id="slider">
<li>
<img src="http://mystuffspace.com/graphic/adorable-puppies.jpg"/>
</li>
<li>
<img src="http://2.bp.blogspot.com/_TP5pGCYHvfg/SyMRSFukqJI/AAAAAAAAA0Y/yIP62DYXOP8/s400/Puppies.jpg"/>
</li>
<li>
<img src="http://temunot.files.wordpress.com/2008/11/4-cute-puppies-wallpaper-640x480.jpg" />
</li>
<li>
<img src="http://mystuffspace.com/graphic/puppies-2.jpg" />
</li>
</ul>
</div>
</body>
</html>
JavaScript :
var margin = 0, slider = $('#slider'), width = 300;
$(document).ready(function() {
setInterval(function() {
slider.trigger('slide');
},2000);
});
slider.bind('slide',function() {
if(margin <= slider.width() *(-1) + width) {
margin = 0;
}else if(margin <= slider.width()) {
margin -= width;
}
slider.animate({marginLeft : margin},500);
});