samedi 25 juin 2016

How do I make ajax stop with setTimeout in a webchat to show a chatline every set time even if they were submitted at the same time


How do I make "case 'chatLine':" wait a certain time before sending the params to print on the screen. I have tried to place a setTimeout() but it does not wait for the time that I want it to. I have even made an ajax function to go into a PHP file that would sleep(10) and then return to the main render case but it can't do this. Don't know why...

How would I set the time to read a chatline every 10 seconds? For example, if 2 or more chatline get submitted at the same time for it to shown in 10 seconds each of them by the order that they went in. lines submitted at the exact same time would be Line1, Line2, and line3:

LINE 1: HELLO

//10 seconds after show LINE 2

LINE 2: HI

//10 seconds after show LINE 3

LINE 3: GREAT

the wait.class.php file is

the full code for this is at:
http://tutorialzine.com/2010/10/ajax-web-chat-php-mysql/
http://tutorialzine.com/2010/10/ajax-web-chat-css-jquery/

That is all I did in the PHP file. What I am making is a web chat in ajax and I want to be able to show

// The login method hides displays the
// user's login data and shows the submit form

login : function(name,gravatar){

    chat.data.name = name;
    chat.data.gravatar = gravatar;
    $('#chatTopBar').html(chat.render('loginTopBar',chat.data));

    $('#loginForm').fadeOut(function(){
        $('#submitForm').fadeIn();
        $('#chatText').focus();
    });

},

// The render method generates the HTML markup
// that is needed by the other methods:

render : function(template,params){



    function wait(){
        $.ajax({
            url: "php/classes/wait.class.php",
            type: "POST",
            data: {
                'text': params.text,
                'time': params.time
            },
        })
    }
    var arr = [];
    switch(template){
        case 'loginTopBar':
            arr = [
            '<span><img src="',params.gravatar,'" width="23" height="23" />',
            '<span class="name">',params.name,
            '</span><a href="" class="logoutButton rounded">Logout</a></span>'];
        break;

        case 'chatLine':
            arr = [
                '<div class="chat chat-',params.id,' rounded"><span class="gravatar">'+
                '<img src="',params.gravatar,'" width="23" height="23" '+
                'onload="this.style.visibility='visible'" />',
                '</span><span class="author">',params.author,
                ':</span><span class="text">',params.text,
                '</span><span class="time">',params.time,'</span></div>'];
        break;

        case 'user':
            arr = [
                '<div class="user" title="',params.name,'"><img src="',params.gravatar,
                '" width="30" height="30" onload="this.style.visibility='visible'"'+
                ' /></div>'
            ];
        break;
    }

    // A single array join is faster than
    // multiple concatenations

    return arr.join('');

},

Aucun commentaire:

Enregistrer un commentaire