Need help with java script - next button on banner not working right
-
:(( Hi Guys, I have incorporated this javascript banner news flash within my .net application, that works fine as it rotates through the news items. End users have recently asked for a next button/link that would immediately display the next item without them having to wait a few seconds. Now, the problem is that when I click on the next button, it doesn't display the next item right away, then the news items start to rotate faster and faster as I click the next button over and over... here is the layout of my silly js: function DisplayBanner(result, context) { ...get xml representation of banner items ...assign variables loop(); } function loop() { ...go through displaying one banner item ...increment banner index setTimeout('loop()', 10000); } function nextBanner() { ...increment banner index loop(); } My next button calls the nextBanner() function while the DisplayBanner() function is called on page load. Could someone please-please-please tell me what is it that Im doing wrong? Thanks for all/any help :-) Nila
-
:(( Hi Guys, I have incorporated this javascript banner news flash within my .net application, that works fine as it rotates through the news items. End users have recently asked for a next button/link that would immediately display the next item without them having to wait a few seconds. Now, the problem is that when I click on the next button, it doesn't display the next item right away, then the news items start to rotate faster and faster as I click the next button over and over... here is the layout of my silly js: function DisplayBanner(result, context) { ...get xml representation of banner items ...assign variables loop(); } function loop() { ...go through displaying one banner item ...increment banner index setTimeout('loop()', 10000); } function nextBanner() { ...increment banner index loop(); } My next button calls the nextBanner() function while the DisplayBanner() function is called on page load. Could someone please-please-please tell me what is it that Im doing wrong? Thanks for all/any help :-) Nila
If you call the loop() function more than once, you will start another loop that runs along side the one that was started initially. Each time you call the loop() function you add another loop, that's why the items rotate faster and faster. I would suggest that you add a counter to the loop and fire it a lot more often. That way you can just zero the counter to make it skip to the next item:
var counter;
function DisplayBanner(result, context) {
...get xml representation of banner items
...assign variables
counter = 100;
loop();
}function loop() {
counter--;
if (counter <= 0) {
...go through displaying one banner item
...increment banner index
counter = 100;
}
window.setTimeout('loop()', 100);
}function nextBanner() {
counter = 0;
}--- b { font-weight: normal; }
-
If you call the loop() function more than once, you will start another loop that runs along side the one that was started initially. Each time you call the loop() function you add another loop, that's why the items rotate faster and faster. I would suggest that you add a counter to the loop and fire it a lot more often. That way you can just zero the counter to make it skip to the next item:
var counter;
function DisplayBanner(result, context) {
...get xml representation of banner items
...assign variables
counter = 100;
loop();
}function loop() {
counter--;
if (counter <= 0) {
...go through displaying one banner item
...increment banner index
counter = 100;
}
window.setTimeout('loop()', 100);
}function nextBanner() {
counter = 0;
}--- b { font-weight: normal; }
Thanks Guffa, it worked like a charm!!! :-D
Nila