ajax调用花了很多时间进行加载。我们在调用data.php之后将数据保存在info_arr变量中,用于在数据库中存储数据。
脚本代码
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);
?>
我如何最小化加载时间?因为本网站的其他功能受到影响
答案 0 :(得分:1)
试试这个:只需从async: false,
更改为async: true,
即可完美运行..