ajax 加载时间太长
ajax taking too much time for loading
ajax 调用花费了很多时间 loading.we 在 info_arr 变量中保存数据,然后调用 data.php 将数据存储在数据库中。
脚本代码
function insert_init_cookie(isclosed){
if(!checkCookie('__utma')){
setCookie('__utma', hash('<?=$domain?>')+'..'+getUniqueId()+'..'+'<?=$unixtime?>'+'..'+'<?=$unixtime?>'+'..'+'<?=$sess_id?>'+'..'+'<?=$sc?>', 0, '/', '<?=$domain?>',false);
}
if(!checkCookie('__Idletime')){
setCookie('__Idletime',idleTime, 3, '/', '',false);
}
var StrCaVal = readCookie('__JDutma') ;
var dn = '<?=$domain?>';
var ca = encodeURIComponent(StrCaVal);
var info_arr = '<?= json_encode($trace_info)?>';
$.ajax({
type : "POST",
async: false,
url : WEBROOT+"cookies/data.php",
data : {
dn : dn,
bclosed:isclosed,
ca : ca,
info_arr : info_arr,
},
});
}
用于在数据库中插入数据 data.php 代码
<?php
session_start();
ob_start();
$pst_data = json_decode(stripslashes($_POST['info_arr']));
$trace_info = json_decode(json_encode($pst_data),true);
$dn = $_POST['dn'];
$ca = $_POST['ca'];
$bclosed = $_POST['bclosed'];
$StrTrackVal = $_POST['StrTrackVal'];
$strca= urldecode($ca);
$strcc= urldecode($cc);
$strca = explode("..",$strca);
$StrTrackVal = explode("..",$StrTrackVal);
global $rt_con,$comp_con;
$comp_con=mysql_connect('9.2.0.203','123','123') or die('error');# connection of
$sql_check = "SELECT uniquevisitorid FROM analytics_staging.tbl_analytics_data WHERE uniquevisitorid = '".$strca[1]."'";
$analycz = mysql_query($sql_check,$comp_con);
if(mysql_num_rows($analycz)<1){
$unix_start_time = strtotime(date('jS F Y h:i:s A (T)'));
$unix_end_time = '';
}else{
$unix_start_time = strtotime(date('jS F Y h:i:s A (T)'));
$unix_end_time = $unix_start_time;
$update_analycz="UPDATE analytics_staging.tbl_analytics_data SET unixtimestamp_end = '".$unix_start_time."' WHERE uniquevisitorid='".$strca[1]."' ORDER BY id DESC LIMIT 1;";
$result = mysql_query($update_analycz,$comp_con);
}
if(empty($bclosed))
$insert_into_analycz="INSERT INTO analytics_staging.tbl_analytics_data (cookiename,domainhash,uniquevisitorid,unixtimestamp_start,unixtimestamp_end,sessionid,session_counter,referral,path,domainname,userip,country,city,browser,useros,querystring,http_host,http_user_agent,inserttime) values('','".$strca[0]."','".$strca[1]."','".$unix_start_time."','','".$trace_info['session_id']."','".$trace_info['sc']."','".$trace_info['referer']."','".$trace_info['path']."','".$dn."','".$trace_info['user_ip']."','".$trace_info['UserCountry']."','".$trace_info['UserCity']."','".$trace_info['UserBrower']."','".$trace_info['UserOS']."','".$trace_info['UserQuery_String']."','".$trace_info['JDHTTP_HOST']."','".$trace_info['user_agent']."',now())";
$result_analycz = mysql_query($insert_into_analycz,$comp_con);
?>
我如何最大限度地减少加载时间?因为这个网站的其他功能受到影响
试试这个:只需将 async: false,
更改为 async: true,
它可能会完美运行..
ajax 调用花费了很多时间 loading.we 在 info_arr 变量中保存数据,然后调用 data.php 将数据存储在数据库中。
脚本代码
function insert_init_cookie(isclosed){
if(!checkCookie('__utma')){
setCookie('__utma', hash('<?=$domain?>')+'..'+getUniqueId()+'..'+'<?=$unixtime?>'+'..'+'<?=$unixtime?>'+'..'+'<?=$sess_id?>'+'..'+'<?=$sc?>', 0, '/', '<?=$domain?>',false);
}
if(!checkCookie('__Idletime')){
setCookie('__Idletime',idleTime, 3, '/', '',false);
}
var StrCaVal = readCookie('__JDutma') ;
var dn = '<?=$domain?>';
var ca = encodeURIComponent(StrCaVal);
var info_arr = '<?= json_encode($trace_info)?>';
$.ajax({
type : "POST",
async: false,
url : WEBROOT+"cookies/data.php",
data : {
dn : dn,
bclosed:isclosed,
ca : ca,
info_arr : info_arr,
},
});
}
用于在数据库中插入数据 data.php 代码
<?php
session_start();
ob_start();
$pst_data = json_decode(stripslashes($_POST['info_arr']));
$trace_info = json_decode(json_encode($pst_data),true);
$dn = $_POST['dn'];
$ca = $_POST['ca'];
$bclosed = $_POST['bclosed'];
$StrTrackVal = $_POST['StrTrackVal'];
$strca= urldecode($ca);
$strcc= urldecode($cc);
$strca = explode("..",$strca);
$StrTrackVal = explode("..",$StrTrackVal);
global $rt_con,$comp_con;
$comp_con=mysql_connect('9.2.0.203','123','123') or die('error');# connection of
$sql_check = "SELECT uniquevisitorid FROM analytics_staging.tbl_analytics_data WHERE uniquevisitorid = '".$strca[1]."'";
$analycz = mysql_query($sql_check,$comp_con);
if(mysql_num_rows($analycz)<1){
$unix_start_time = strtotime(date('jS F Y h:i:s A (T)'));
$unix_end_time = '';
}else{
$unix_start_time = strtotime(date('jS F Y h:i:s A (T)'));
$unix_end_time = $unix_start_time;
$update_analycz="UPDATE analytics_staging.tbl_analytics_data SET unixtimestamp_end = '".$unix_start_time."' WHERE uniquevisitorid='".$strca[1]."' ORDER BY id DESC LIMIT 1;";
$result = mysql_query($update_analycz,$comp_con);
}
if(empty($bclosed))
$insert_into_analycz="INSERT INTO analytics_staging.tbl_analytics_data (cookiename,domainhash,uniquevisitorid,unixtimestamp_start,unixtimestamp_end,sessionid,session_counter,referral,path,domainname,userip,country,city,browser,useros,querystring,http_host,http_user_agent,inserttime) values('','".$strca[0]."','".$strca[1]."','".$unix_start_time."','','".$trace_info['session_id']."','".$trace_info['sc']."','".$trace_info['referer']."','".$trace_info['path']."','".$dn."','".$trace_info['user_ip']."','".$trace_info['UserCountry']."','".$trace_info['UserCity']."','".$trace_info['UserBrower']."','".$trace_info['UserOS']."','".$trace_info['UserQuery_String']."','".$trace_info['JDHTTP_HOST']."','".$trace_info['user_agent']."',now())";
$result_analycz = mysql_query($insert_into_analycz,$comp_con);
?>
我如何最大限度地减少加载时间?因为这个网站的其他功能受到影响
试试这个:只需将 async: false,
更改为 async: true,
它可能会完美运行..