Javascript的功能与php相同?
我正在使用WebIM在网站上提供聊天支持。我希望能够为客户端启动聊天会话设置计时器,如果操作员/技术人员在“x”秒内没有响应,我希望页面重定向到另一个页面,客户端可以在该页面上留言 比如“请稍等,我们试着联系你”之类的话。这样,如果所有的技术人员都太忙或正在帮助其他客户,等待的客户可以稍后再试或留下消息(当聊天处于脱机状态时) 我仔细查看了mibew.org这个聊天脚本的创建者,没有任何关于这个功能的内容,而且看起来他们的网站已经被废弃了 我想出了一个主意,使用Javascript setTimeout函数在“x”之后运行一些php。php基本上会查询DB,查看是否有技术人员进入了讨论中的会话,如果没有,则将客户端重定向到另一个页面,解释目前没有人可用,但他们可以留下消息,等等 问题是,我对JS没有多少经验 这有可能吗?还有没有比这更有效的方法呢?是的,你可以做到 您可以使用timeout函数每x秒执行一次javascript函数Javascript的功能与php相同?,php,javascript,function,redirect,timer,Php,Javascript,Function,Redirect,Timer,我正在使用WebIM在网站上提供聊天支持。我希望能够为客户端启动聊天会话设置计时器,如果操作员/技术人员在“x”秒内没有响应,我希望页面重定向到另一个页面,客户端可以在该页面上留言 比如“请稍等,我们试着联系你”之类的话。这样,如果所有的技术人员都太忙或正在帮助其他客户,等待的客户可以稍后再试或留下消息(当聊天处于脱机状态时) 我仔细查看了mibew.org这个聊天脚本的创建者,没有任何关于这个功能的内容,而且看起来他们的网站已经被废弃了 我想出了一个主意,使用Javascript setTim
var time = 200;
setTimeout(function()
{
func();
}, time);
然后让函数执行ajax调用,从数据库中请求一些结果,并根据结果执行一些操作。这一点都不难。当超时方法规定:
$.post('url/of/phpscript',
$data /* data you'd like to send*/,
function(returnData) { doSomethingWithThisPhpValue(data) });
function doSomethingWithThisPhpValue(data) {
if (data == signed_in) //...
}
当然,值测试和变量最终将取决于您从php方法返回的数据,因此我不能说得更准确了。类似的方法可以工作
function callAfter5Seconds() {
// your redirect or any other work goes here
}
// the second argument is the time, in milli-seconds (5000 = 5 seconds)
var myTimeout = setTimeout(callAfter5Seconds, 5000);
。。。如果接线员不接电话,别忘了取消超时
clearTimeout(myTimeout);
下面是一个示例,说明如何使用javascript和PHP协同工作,而不将两者混淆(ajax): 首先,您需要一个javascript函数:
function example()
{
var select = true;
var url = '../scripts/ajax.php';
$.ajax(
{
// Post select to url.
type : 'post',
url : url,
dataType : 'json', // expected returned data format.
data :
{
'select' : select // the variable you're posting.
},
success : function(data)
{
// This happens AFTER the PHP has returned an JSON array,
// as explained below.
var result1, result2, message;
for(var i = 0; i < data.length; i++)
{
// Parse through the JSON array which was returned.
// A proper error handling should be added here (check if
// everything went successful or not)
result1 = data[i].result1;
result2 = data[i].result2;
message = data[i].message;
// Do something with result and result2, or message.
// For example:
$('#content').html(result1);
}
},
complete : function(data)
{
// do something, not critical.
}
});
}
如果未设置$select的值,则三元运算符会使其变为false
确保您可以在此处访问您的数据库:
$db = $GLOBALS['db']; // En example of a PDO database connection
现在,检查是否请求了$select(true),然后执行一些数据库请求,并用JSON返回它们:
if($select)
{
// Fetch data from the database.
// Return the data with a JSON array (see below).
]
else
{
$json[] = array
(
'message' => 'Not Requested'
);
}
echo json_encode($json);
flush();
if($select)
{
// Assume that the previously defined query exists.
$results = getSingleRow($db, $query);
if($results !== false)
{
$json[] = array
(
'result1' => $results['result1'],
'result2' => $results['result2'],
'message' => 'success'
);
}
else // Nothing found in database
{
$json[] = array
(
'message' => 'nothing found'
);
}
}
// ...
当然,从数据库获取数据的方式是可选的,您可以使用JSON从数据库获取一行数据,也可以使用JSON返回多行数据
让我举一个例子,说明如何使用json返回多行(您将在javascript(数据)中迭代):
然后你可以这样做:
if($select)
{
$array = array();
$i = 0;
$query = 'SELECT e.result1, e.result2 FROM exampleTable e ORDER BY e.id ASC;';
foreach(selectMultipleRows($db, $query) as $row)
{
$array [$i]["result1"] = $row['result1'];
$array [$i]["result2"] = $row['result2'];
$i++;
}
if(!(empty($array))) // If something was fetched
{
while(list($key, $value) = each($array))
{
$json[] = array
(
'result1' => $value["result1"],
'result2' => $value["result2"],
'message' => 'success'
);
}
}
else // Nothing found in database
{
$json[] = array
(
'message' => 'nothing found'
);
}
}
// ...
或者,如果你想接吻(保持简单愚蠢):
Init一个基本函数,从数据库中选择一些值并返回一行:
function getSingleRow($db, $query)
{
$stmt = $db->prepare($query);
$stmt->execute();
// $stmt->execute(array(":id"=>$someValue)); another approach to execute.
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if($result)
{
$array = (
'result1' => $result['result1'],
'result2' => $result['result2']
);
// An array is not needed for a single value.
return $array;
}
return false;
}
然后获取行(或单个值)并用JSON返回:
if($select)
{
// Fetch data from the database.
// Return the data with a JSON array (see below).
]
else
{
$json[] = array
(
'message' => 'Not Requested'
);
}
echo json_encode($json);
flush();
if($select)
{
// Assume that the previously defined query exists.
$results = getSingleRow($db, $query);
if($results !== false)
{
$json[] = array
(
'result1' => $results['result1'],
'result2' => $results['result2'],
'message' => 'success'
);
}
else // Nothing found in database
{
$json[] = array
(
'message' => 'nothing found'
);
}
}
// ...
不管怎样,我希望我不是因为这么精确而射中了自己的腿,但我喜欢这种技术,@Sabyre想要一个例子,就在这里。如果您发现有什么奇怪的事情发生,请发表评论。setTimeout是window对象中内置的函数,您不需要jQuery来完成it@Jonast92setTimeout()将在经过指定时间后执行回调。要每x秒执行一次函数,需要使用setInterval(),非常感谢您提供的这篇文章。几个小时后,我终于用这个得到了它:
var-time=150000;setTimeout(function(){if(''='n')window.location.replace('http://stackoverflow.com","时间",代码>酷,但是你应该考虑不混合PHP和JavaScript(理想情况下它不应该工作),而是执行一些从PHP脚本接收一些信息的Ajax调用。但如果它能工作,那就太好了:)我不知道怎么做。。。我知道PHP,java/ajax,但我几乎没有触及表面。你能重新编一个好地方开始吗?setTimeout($.post('libs/newchattimer.php',function(returnData){ReDirect(data)});function ReDirect(data){if(data==“n”)window.location.replace('http://stackoverflow.com"); }, 500);代码>我打开了错误报告,但什么也没有得到<代码>错误报告(E_全部);ini_集('display_errors','1')这可能是因为您的ajax查询由于javascript错误而失败。使用returnData,然后尝试传递下一个方法数据。解决你的javascript错误。我确信Google Chrome的开发者控制台将帮助你调试javascript错误,我相信大多数浏览器都有这样的功能,你必须查找特定的浏览器并使用它。Jonat,再次感谢你。。。你真是太好了,给一个明显的新手提供了如此深刻的解释。我希望我有足够的空间发布我使用的实际代码和流程。我也是这个网站的新手。基本上,我使用ajax在x时间之后运行一个php,该php创建了一个平面文件,ajax读取该文件以确定聊天是否应答。有了js,我真是个新手。如果你想查看聊天记录并与我交谈,我工作的网站是[链接](www.itdsi.com)。再次感谢@