www.webdeveloper.com
Results 1 to 11 of 11

Thread: Run ajax script in real time

  1. #1
    Join Date
    Oct 2013
    Posts
    6

    Question Run ajax script in real time

    Is there a way to make this script run in real-time, rather than using setInerval()? I do not know where to begin.

    Code:
    $(window).load(function(){
        setInterval(function(){
        var username=_userdata['username'];
        var username=_userdata['username'];
        $.get('/notification.forum',function(data){
             var b= data.unread;
            if(b >= 1){
              var c = data.store;
              for(var keys in c){
                var d = c[keys];
               if(d.read === 0){
                var e= d.text.type;
                var from= d.text.from.name;
                var topic = e===7 ? "in the topic "+d.text.post.topic_name.replace(/-/g,' ') : "";
                var type= Notify(e);
                  createNotifcation('http://i.imgur.com/LPVUvcf.png',username + ' you have a new notification!',type + " from "+from+"  "+topic);
               }
              }
             }
           });
        },5000);
           });
    Edit:
    I found this:

    Code:
    function listen() {
            $.get("/mylongrequestfile", {}, function(data) {
            	$("#mydiv").html(data);
            	listen(); // then launch again
            }));
        };
    How do i include this into my script ? Or how to i make this in real time.. (my initial script..)

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,137
    You have the interval set to 5 seconds, you just lower that timer value to 1000 to make a request to the server every second.

    You also have the issue of the number of users that you would be servicing, this multiplied up would put the server under allot of load if your requests were being run at a higher frequency.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Oct 2013
    Posts
    6
    Quote Originally Posted by \\.\ View Post
    You have the interval set to 5 seconds, you just lower that timer value to 1000 to make a request to the server every second.

    You also have the issue of the number of users that you would be servicing, this multiplied up would put the server under allot of load if your requests were being run at a higher frequency.

    This is the problem. I want another method for this, like chat's method..

  4. #4
    Join Date
    Oct 2013
    Posts
    6
    Bumpp ! Is this forum dead ?

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,137
    You set another interval timer to call the process you want updated. Job done. The problem is the frequency at which you make the calls and that corresponds to load and users, the more of a load, the slower the system and more prone to users injecting code to bring the servers down.

    As for your last response, no this forum isn't dead, your prolly asking the question the wrong way and not many people regard JQuery as programming or JavaScript.

    I don't use JQ at all, I have intentionally ignored it.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  6. #6
    Join Date
    Oct 2013
    Posts
    6
    So, doesn't exists another method to call a function in real time, rather than using setInterval() ?

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,137
    How do you think it runs? Without an even timer set to poll the server at set intervals, you won't have any kind of server / client interaction.

    You also have to have a new AJAX object made per call to the server, so client side you are looking at a separate routine to do the legwork to your display services.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  8. #8
    Join Date
    Oct 2013
    Posts
    6
    I'm sure it's a method, rather than setInterval(). BUMPPP !

  9. #9
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,137
    1. you have a setInterval timer that calls your AJAX fetch script
    2. create a new instance of an AJAX object
    3. you poll your server script that returns the information you requested
    4. your JavaScript handler then updates your display routine
    5. your setInterval timer then calls the function that repeats steps 2,3,4 until the client using the service leaves.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  10. #10
    Join Date
    Oct 2013
    Posts
    6
    How to:
    2,3,4 ?

  11. #11
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,137
    If you search how to make an AJAX object (look for the work around method) then you will see that adding a setInterval timer is easy and that the returned object data can be then easily inserted in to the field you want to display the results in.

    You are basically doing something like this.
    Code:
    // Your chosen XMLHttpRequest work around of choice 
    if(!typeof XMLHttpRequest=='function'){
        XMLHttpRequest = function(){
            // the contents of this function will rely on you doing some homework.
            // ...
            return new xmlhttpobject;
        }
    }
    
    
    method = "get";
    resource = "your_script_in.php?" + new Date().getTime();
    async = true;
    
    function updateDisplay(){
        ajax = new XMLHttpRequest();
        ajax.open(method,resource,async);
        ajax.onreadystatechange = eventHandler;
        ajax.send();
    }
    
    function eventHandler(){
        // your test for if the event call reaches completion
        if( ajax.readyState==4 && ajax.status==200 ){
            // your completion function, what do you want to do with the data
            alert( ajax.responseText);
        }
    }
    
    ajaxAuto = setInterval("updateDisplay()",3000); // make a request every 3 seconds
    Thats a rough outline of the process. You will need to iron out any kinks in it...
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

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