www.webdeveloper.com
Results 1 to 6 of 6

Thread: help with setinterval ajax function

  1. #1
    Join Date
    Jul 2007
    Posts
    15

    help with setinterval ajax function

    I am using the following ajax jquery function to track scrolling. It works fine for me. Pay no attention to the %% parameters they are substituted at run time.

    Code:
    <script type="text/javascript">
    $(document).ready(function(){
        setInterval(function() {
     		x=GetScrolledTop();
     		y=document.body.scrollHeight;
     		z=$(window).height();
     		j=$(document).height();
            jQuery.ajax({
                type: "GET",
                url: "scrollposition.blp?z="+x+"-"+y+"-"+z+"-"+j+"&bar="+%barid%+"&lid=%landingid%"+"&sid=%sessionid%"+"&sh="+screen.height+"&sw="+screen.width ,
                dataType:"json",
                success:function(response){
    	        }
            });
        }, 5000);
    });

    I want to limit this function to run for a total specified time max. For example, this snippet polls every 5 seconds. I need to have this function stop after x polls or x minutes total to prevent a rogue page which gets left open in a browser from consuming useless resources. What is the easiest way to accomplish this?

    Thanks in advance for any assistance...

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,001
    There are a couple of small changes you can make to get what you want. I think you should add 2 global variables (outside of the document ready check in jQuery), one to hold your interval and the other to keep a count.

    The interval variable is simply to allow you to use clearInterval() once the maximum number of polls has been reached. The second variable obviously will keep track of the number of polls made and thus will allow you to clear the interval once a condition has been met.

    I've added a 3rd global variable in the code below for ease-of-use in updating. This 3rd variable is merely to allow you to set a max runtime (in minutes). You can modify this code to simply run based on the number of polls, or any other metric that you want to use.
    Code:
    var $interval;
    var $pollCount = 0;
    var $maxRuntime = 1; // This is in minutes
    $(document).ready(function(){
    	$interval = setInterval(function() {
    		if($pollCount >= ($maxRuntime * 12)) {
    			clearInterval($interval);
    			return;
    		}
    		x=GetScrolledTop();
    		y=document.body.scrollHeight;
    		z=$(window).height();
    		j=$(document).height();
    		jQuery.ajax({
    			type: "GET",
    			url: "scrollposition.blp?z="+x+"-"+y+"-"+z+"-"+j+"&bar="+%barid%+"&lid=%landingid%"+"&sid=%sessionid%"+"&sh="+screen.height+"&sw="+screen.width ,
    			dataType: "json",
    			success: function(response){
    			}
    		});
    		$pollCount++;
    	}, 5000);
    });

  3. #3
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Another way you could approach it is to only transmit the data on the initial hit, then if they move on the page at all:

    Code:
    <script type="text/javascript">
    
    $(document).ready(function()
    {
    	var last = '';
    	setInterval(function()
    	{
    		var link =
    			"scrollposition.blp?z=" +
    			GetScrolledTop() + "-" +
    			document.body.scrollHeight + "-" +
    			$(window).height() + "-" +
    			$(document).height() +
    			"&bar=" + %barid% + "&lid=%landingid%" + "&sid=%sessionid%" + "&sh=" + screen.height + "&sw=" + screen.width;
    
    		if (last !== link) {
    			last = link;
    			jQuery.ajax({
    				type     : "GET",
    				url      : link,
    				dataType : "json",
    				success  : function(response) {}
    			});
    		}
    	}, 5000);
    });
    
    </script>
    This code is untested.

  4. #4
    Join Date
    Jul 2007
    Posts
    15
    Thanks. This is just what I was looking for... I will give these both a ride

  5. #5
    Join Date
    Jul 2007
    Posts
    15
    got first one to work as expected, but the second (which made sense) wasn't doing what I expected. Haven't had time to debug it... just wanted to say thanks again tho!

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Does it not work at all, or just not the way you need it to?

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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