www.webdeveloper.com
Results 1 to 5 of 5

Thread: setTimeout and setInterval insane speeds

  1. #1
    Join Date
    Aug 2010
    Posts
    3

    Exclamation setTimeout and setInterval insane speeds

    Hi guys, I'm new to this forum, so be nice

    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
    }
    //-->

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    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...

  3. #3
    Join Date
    Aug 2010
    Posts
    3

    Solved!

    Thx, had a look at the code...
    function Loadcontent now ends like this, and it works!!

    Code:
    //blahblahblah, same as above
    	            teller = (teller+1);
    		}
    		//reLoad (teller);
    		var id = setInterval(function ()
    		{
    			loadContent (teller);
    			clearInterval(id);
    		}
    		, 5000);
    	}

  4. #4
    Join Date
    Aug 2010
    Posts
    3

    Damn and blast

    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...

    How do I tackle this?

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles