jQuery + ThinkPHP + Ajax achieve instant message alerts

Whim want to do a reminder system for their own small projects, such as private letters, comments and other information can be promptly passed over. Since Daoxing still shallow, those long online poll for me a little complicated, so that a write or write your own try better.

My idea is to build a separate reminder table in the database table consists of two fields id and the recipient of the message type composition

*   Reception alert table   * / 
the CREATE  TABLE  the IF  the NOT  EXISTS Notification ( 
    ID            the INT  the NOT  NULL the AUTO_INCREMENT a PRIMARY  KEY ,     -     the primary key increment 
    MID          the INT  the NOT  NULL  the DEFAULT  0 ,                         -     user ID 
    type         the INT  the NOT  NULL  the DEFAULT  0                         -   0: 1 private letter: post comment 2: 3 goods sold: product reviews 4: group request has been sent 5: Creating a successful team 6: 7 new members to join the group: the old team members exit 8: invitations to events 
) ENGINE = MyISAM the DEFAULT CHARSET=UTF8;

Then write a recursive function in the front page for Ajax requests

function  the require () {
       var URL = "{the U-( 'Group / checkNotify')}" ; 
          
      $ .get (URL, null , function (Data) { 
              
            // If the data obtained is not empty, the alert displays 
           IF ($ . TRIM ! (Data) = '' ) { 

               // here written reminder manner 
            alert ( 'haha' ); 
           } 
      }); 

      // every three seconds time request 
      the setTimeout ( 'the require ()', 3000 ); 
}

In three seconds before the background check database to the current time if data is inserted, and if so, the required information returns

public  function checkNotify () { 

    // instantiate the model class definition from 
    $ a Notify = D ( "the Notification" ); 

    // get the current user id 
    $ MID = $ _SESSION [ 'Member'] [ 'id' ]; 

    // Because Ajax only three seconds execution time, so the time to insert new data later than the query request time (the current time) for three seconds 
    $ time = time () - 3 ; 

    // prepare the query 
    $ the WHERE = "MID = MID $ and the Created> $ Time " ; 

     // find out if there is new data into the database 
    $ BOOL = $ the Notify -> the WHERE ( $ the WHERE ) -> the Find (); 
        
    //If the query result is not empty, then set the output result of data type parameter of zero, i.e., the type of alert, and then acquires the contents from a database corresponding to the alert table 
    // default this test only three seconds within a message, if you want more precise, request time may be shortened 
    IF ( $ BOOL =! null ) { 

        // test data           
        echo  $ BOOL [0] [ 'type' ]; 
    }; 
}

Of course, to make the document load is complete after the execution of the function

<body onload="javascript:return require();">

My idea is to build a separate reminder table in the database table consists of two fields id and the recipient of the message type composition

*   Reception alert table   * / 
the CREATE  TABLE  the IF  the NOT  EXISTS Notification ( 
    ID            the INT  the NOT  NULL the AUTO_INCREMENT a PRIMARY  KEY ,     -     the primary key increment 
    MID          the INT  the NOT  NULL  the DEFAULT  0 ,                         -     user ID 
    type         the INT  the NOT  NULL  the DEFAULT  0                         -   0: 1 private letter: post comment 2: 3 goods sold: product reviews 4: group request has been sent 5: Creating a successful team 6: 7 new members to join the group: the old team members exit 8: invitations to events 
) ENGINE = MyISAM the DEFAULT CHARSET=UTF8;

Then write a recursive function in the front page for Ajax requests

function  the require () {
       var URL = "{the U-( 'Group / checkNotify')}" ; 
          
      $ .get (URL, null , function (Data) { 
              
            // If the data obtained is not empty, the alert displays 
           IF ($ . TRIM ! (Data) = '' ) { 

               // here written reminder manner 
            alert ( 'haha' ); 
           } 
      }); 

      // every three seconds time request 
      the setTimeout ( 'the require ()', 3000 ); 
}

In three seconds before the background check database to the current time if data is inserted, and if so, the required information returns

public  function checkNotify () { 

    // instantiate the model class definition from 
    $ a Notify = D ( "the Notification" ); 

    // get the current user id 
    $ MID = $ _SESSION [ 'Member'] [ 'id' ]; 

    // Because Ajax only three seconds execution time, so the time to insert new data later than the query request time (the current time) for three seconds 
    $ time = time () - 3 ; 

    // prepare the query 
    $ the WHERE = "MID = MID $ and the Created> $ Time " ; 

     // find out if there is new data into the database 
    $ BOOL = $ the Notify -> the WHERE ( $ the WHERE ) -> the Find (); 
        
    //If the query result is not empty, then set the output result of data type parameter of zero, i.e., the type of alert, and then acquires the contents from a database corresponding to the alert table 
    // default this test only three seconds within a message, if you want more precise, request time may be shortened 
    IF ( $ BOOL =! null ) { 

        // test data           
        echo  $ BOOL [0] [ 'type' ]; 
    }; 
}

Of course, to make the document load is complete after the execution of the function

<body onload="javascript:return require();">

Guess you like

Origin www.cnblogs.com/chenduzizhong/p/10985753.html