I've been trying a number of different approaches but can't seem to get it right. The idea was to loop the main funtion of the script (loadContent) by ending with the setTimeout, that I had to move to another function. Why, I don't know, but that's the only way it works.
I'm a professional PHP programmer, getting to grips with Javascript in this era where Ajax rules firmly is part of my job. Can't say I have been enjoying the experience so far, though.
Right, enough bashing for today! The idea was to have a simple script, linked to a php script that would load images, based on the data sent via Javascript and an XML datafile.
This does happen, only: the timeout of 5 or 6 seconds doesn't seem to last even a millisecond... I've googled around, I'm not the only one to have encountered this issue.
Code:
<!--
//global var
var track = null;
function loadContent (count)
{
var ajax = false;
if (window.XMLHttpRequest)
{
ajax = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
try
{
ajax = new ActiveXObject ("Msxml2.XMLHTTP");
}
catch (e1)
{
try
{
ajax = new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (e2)
{
alert("Your browser does not support AJAX.");
ajax = null;
}
}
}
if (ajax)
{
ajax.open('get', 'ajax.php?slide=' + count, true);
ajax.onreadystatechange = function ()
{
if ((ajax.readyState == 4) && (ajax.status == 200))
{
document.getElementById('slide').innerHTML = ajax.responseText;
}
}
ajax.send(null);
if (count >=6)
{
count= 1;
}
else
{
count= (count+1);
//looks silly, different syntaxes for all used languages help me to switch back and forth between html, php, perl and javascript without typo's and syntax errors
}
reLoad (count);
}
else
{
document.getElementById('slide').innerHTML = 'Your Browser does not support Ajax.';
}
}
//later...
function reLoad (slide)
{
track = setInterval(loadContent (slide), 6000);
//setTimeout (loadContent (slide), 5000); => this doesn't work either
}
//-->
you call reLoad from loadContent.
you also never cancel the repeating interval, so you probably have several all firing at once.
use a single interval or cancelable timeouts...
So, ok... the script works, but now I've been adding some functions like: pause and play button, browse buttons (1-2-3...) and pause on rollover. The problem is, the timeout or setInterval doesn't do anything, but still keeps ticking away. so when you're done browsing, the slideshow sometimes fires two or three XHR's @ once...
simply clearing the interval/timeout won't cancel the asynch ajax you already dispatched.
i would have a global named something like "busy" that i checked immediately before making an ajax call. you can turn busy on/off as needed to prevent more than one ajax call.
Bookmarks