here, I have created 4 demo ads. I have also created 2 hidden variables that will help me determine the number of ads and current ad that is displayed.
Note: We can achieve this without creating hidden variables but I am supposing that you are creating these ads dynamically from some server side language. In that case the number of ads can go up and down
Let write following jQuery function in $(document).ready, so that ads wont start refreshing before page loads
Here, first we check if current ad is last one, if yes, we need to start the loop from first element, if no, then it next ad will appear. change the values of hidden variables accordingly. I used interval of 10 sec to change the ad, one can change this value according to the need.