www.webdeveloper.com
Results 1 to 3 of 3

Thread: Ajax Update help

  1. #1
    Join Date
    Jun 2009
    Posts
    2

    Ajax Update help

    Can somebody help me fix this Javascript Ajax script..

    Code:
    function createRequestObject() {
      var ro;
      var browser = navigator.appName;
      if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        ro = new XMLHttpRequest();
      }
      return ro;
    }
    
    var feedUpdate = new Array();
    var feedCount = 0;
    var feedHttp = createRequestObject();
    var lastFeed = null;
    var lastFeedTime = 0;
    var lastTopicTime = 0;
    var lastTopic = null;
    var response = null;
    
    function sndFeedReq() {
    	feedHttp.open('post', 'grab.php');
    	feedHttp.onreadystatechange = handleFeedResponse;
    	feedHttp.send(null);
    }
    
    function handleFeedResponse() {
    	if (feedHttp.readyState == 4) {
    		response = feedHttp.responseText;
    		feedUpdate = response.split('|');
    		rotateFeed();
    	}
    }
    
    /*
    	feedUpdate should now contain:
    		0 => topic
    		1 => topic time
    		2 => feed
    		3 => feed time
    */
    
    function rotateFeed() {
    	if (feedUpdate[0] != lastTopic || lastTopic == null || lastTopic == '') {
    		if (feedUpdate[1] >= lastTopicTime) {
    			lastTopicTime = feedUpdate[1];
    			lastTopic = feedUpdate[0];
    			if (document.getElementById("topic")) {
    				document.getElementById("topic").innerHTML = "Topic: " + feedUpdate[0];
    			}
    		}
    	}
    	
    	if (feedUpdate[2] != lastFeed || lastFeed == null || lastFeed == '') {
    		if (feedUpdate[3] >= lastFeedTime) {
    			lastFeed = feedUpdate[2];
    			lastFeedTime = feedUpdate[3];
    			if (document.getElementById("logo")) {
    				document.getElementById("logo").innerHTML = feedUpdate[2];
    			}
    		}
    	}
    	setTimeout("sndFeedReq()", 10000);
    }
    
    sndFeedReq();
    The problem seems to be at the two if's like this:
    Code:
    if (feedUpdate[2] != lastFeed || lastFeed == null || lastFeed == '') {
    Thanks

  2. #2
    Join Date
    May 2009
    Posts
    150
    You've said there's a problem and where you think it is, but not actually said what the problem is - what it should do and what it does do...

    Two problems I do see are that if the new feed is null it will try to display it first time, and if (unlikely) the request takes longer than 10 seconds the next request will be fired on the same (global) variables.

  3. #3
    Join Date
    Jun 2009
    Posts
    2
    It's supposed to open grab.php, split the |'s and place some info into the main site.. And I also needed the script to be able to push new info to every user in real time.. To make it more specific.. It grabbing the live feed html and the current Chat topic from grab.php.. everytime the feed or topic changes.. i want that change to show without the user refreshing the page..

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