简体   繁体   English

仅在将新记录插入数据库时​​设置ajax调用

[英]Set ajax call only when the new record inserted to database

I wants to get updated comment from chat list to the page without refreshing the page, Thats I have used ajax call for the list but I have to call this function for every 5 seconds to check whether new chat is inserted or not to the database, 我想在不刷新页面的情况下从聊天列表中获取更新的评论,这就是我使用ajax调用列表的原因,但是我必须每隔5秒调用一次此功能,以检查是否将新的聊天插入到数据库中,

$(document).ready(function(){
    setInterval(function(){
        $.ajax({
            type:'POST',
            url: baseUrl+"chat",
            cache: false,
            data: dataString,
            crossDomain: true,
            success: function(data){
                var getData = JSON.parse(data);
                if(getData.status == "success")
                {   
                    for(i=0;i<getData.chat.length)
                    {
                        $("chatList").text("");
                        $("chatList").append("<span class='black'>"+getData["chat"][i].name+" : <span class='blue'>"+getData["chat"][i].comment+"</span>");
                    }
                }
                else
                {
                    alert(getData.message);
                } 
            }
        });
    },5000);
});

So I wants to know if there is any easy way to do this or from PHP MySQL it is possible to send page a new comment inserted notification ? 所以我想知道是否有任何简单的方法可以做到这一点,或者可以从PHP MySQL中发送新的注释插入通知页面?

Best practice is to use HTML5 WEB WORKERS . 最佳做法是使用HTML5 WEB WORKERS

HTML5 Web Workers HTML5网络工作者

The problem with JavaScript on the browser is that it runs on a single thread. 浏览器上JavaScript的问题在于它在单个线程上运行。 In other words, two scripts or processes cannot run simultaneously. 换句话说,两个脚本或进程不能同时运行。 If you are processing JavaScript after page load, the end user cannot interact dynamically with the page. 如果要在页面加载后处理JavaScript,则最终用户无法与页面动态交互。 The JavaScript will not handle UI events while processing something else. JavaScript在处理其他内容时将不会处理UI事件。 If you process something large before page load, the end user has to wait all-together which is a horrible user experience. 如果您在页面加载之前进行了较大的处理,则最终用户必须全部等待,这真是糟糕的用户体验。

You can use a websocket, socket.io for example. 您可以使用websocket,例如socket.io。 That will allow you to send notifications from the server to the client. 这样您就可以将通知从服务器发送到客户端。 So, when you recieve data from your chat (cient) in your API (server), after updating the database, you will have to send a 'notification' to your client. 因此,当您从API(服务器)中的聊天记录(古代)中接收数据时,更新数据库后,您将必须向客户端发送“通知”。

When your client get the notification, you can make your AJAX call : 当客户收到通知时,您可以拨打AJAX电话:

socket.on('notification', function(){
  doYourAJAXStuff();
});

您可以使用socket.io api将实时信息获取到客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用node js和socket.io在mysql数据库中插入新记录时如何通知和更新客户端 - How to inform and update a client when a new record inserted in mysql database using node js and socket.io 刷新div的每次新记录插入mysql数据库 - Refreshing div's everytime new record is inserted in mysql database 记录插入到Mysql数据库后如何重定向到新的HTML页 - How to Redirect to new Html Page After Record Inserted to Mysql database 在数据库中插入新数据时警告用户 - Warn user when new data is inserted on database 使用jquery和ajax插入相同记录时如何更新表 - How to update the table when same record is inserted using jquery and ajax 为什么Capybara与AJAX调用后插入的新DOM元素不匹配? - Why is Capybara not matching new DOM elements inserted after an AJAX call? 插入新记录时如何自动刷新数据表? - How to auto refresh data table when new record is inserted? 使用ajax将数据插入数据库时​​如何显示警报消息? - How to show an alert message when data is inserted to database using ajax? 添加新的相关记录时调用插件 - Call plugin when new related record added CRM 2011-插入新记录或修改现有记录时捕获子网格的事件 - CRM 2011 - Catch events of subgrid when a new record is inserted or an existing record is modified
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM