如何制作" case'chatline':"等待一定时间,然后发送访问访问在屏幕上。我试图放置一个setTimeout()
,但它不等待我想要的时间。我什至做出了一个AJAX函数,以进入将sleep(10)
的PHP文件,然后返回到主渲染案例,但不能这样做。不知道为什么...
我将如何设置时间每10秒阅读一次聊天线?例如,如果2个或更多聊天线同时提交,以通过他们进入的顺序在10秒钟内显示。同时提交的行是Line1,Line2和Line3:
第1行:你好
//节目2之后10秒
第2行:嗨
//节目第3行后10秒
第3行:伟大
wait.class.php文件是
此的完整代码是:
http://tutorialzine.com/2010/10/ajax-web-chat-php-mysql/
http://tutorialzine.com/2010/10/ajax-web-chat-css-jquery/
这就是我在PHP文件中所做的。我正在做的是Ajax中的网络聊天,我希望能够显示
// 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('');
},
您的代码中有两个问题:
-
jquery.ajax被异步化,因此JavaScript代码仍在没有等待服务器响应的情况下继续执行。有一个可以将其更改为同步的参数,但是您的服务器会堆叠大量的睡眠请求,这些请求会吸收很多资源。因此,不要使用此方法。
-
您的" ARR"变量总是每次渲染时都有一个项目,因此无需使用" ARR",只需返回HTML字符串。
这是我的解决方案:
-
从服务器接收到数据后,而不是立即呼叫渲染,将"模板"one_answers"参数"添加到全局数组中。
var items = []; items[] = { 'template': template, 'params': params }; // Begin render and display data if process isn't running // Note: "running" variable is defined in (2.) if(running == false) { display(); }
-
创建一个"显示"函数做两个事情:调用"渲染"以创建html字符串并决定何时将该字符串添加到文档中。
var running = false; function display() { running = true; var item = null; var html = []; while(items.length > 0) { item = items.shift(); // Render HTML string html[] = render(item.template, item.params); // Stop render and output HTML data if(item.template == 'chatLine') { break; } } // A single array join is faster than multiple concatenations html = html.join(''); // TODO: Add rendered HTML string to document ... if(items.length > 0) { // Call "display" again after 10 seconds setTimeout(display, 10000); } else { // There is no item need to render then stop process running = false; // Note: Next time received data from server // "display" will be called as in (1.) } }
我如何制作" case'chatline':"等待一定时间在发送之前 要在屏幕上打印的参数。
您可以使用回调函数,例如
render : function(template,params, callback){
,然后
case 'chatLine':
setTimeout(function(){ callback(arr); }, 10000);
您必须将所有逻辑移动到发送的"回调"作为参数的函数中。另外所有情况都应调用函数回调。
render(template, params, callback);
function callback(results){
//process results, print on the screen
}
我可能很难解释:)
查看script.js第五部分。设置时间安排的变量为 nextRequest
。
在代码的这一部分中,您可以根据最新聊天活动以来的时间来设置nextRequest
。在这里,您可以修改时间以适合您的需求。
摘录摘自:script.js-第5部分
var nextRequest = 1000;
// 2 seconds
if(chat.data.noActivity > 3){
nextRequest = 2000;
}
if(chat.data.noActivity > 10){
nextRequest = 5000;
}
// 15 seconds
if(chat.data.noActivity > 20){
nextRequest = 15000;
}