先直接贴代码
ajax1的调用方如下: var xmlHttp1;
function createXMLRequest()
{
if(window.ActiveXObject)
{
xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp1=new XMLHttpRequest();
}
else
{
alert("不能创建XMLHttpRequest");
}
}
function startRequest(method,url,pars,callbackfun,cache)
{
if(typeof callbackfun != 'function'){
alert('The CallBackFun is not a function!');
}
if(cache){
pars += "&tmp="+Math.random();
}
createXMLRequest();
xmlHttp1.onreadystatechange = function(){
if(xmlHttp1.readyState==4)
{
if(xmlHttp1.status==200)
{
callbackfun(xmlHttp1);
xmlHttp1.abort();
}
else
{
xmlHttp1.abort();
alert("请求出错!");
}
}
}
xmlHttp1.open(method,url+'?'+pars,true);
xmlHttp1.send(null);
}
ajax1的调用方如下: var xmlHttp2;
function createXMLRequestE()
{
if(window.ActiveXObject)
{
xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp2=new XMLHttpRequest();
}
else
{
alert("不能创建XMLHttpRequest");
}
}
function startRequestE(method,url,pars,callbackfun,cache)
{
if(typeof callbackfun != 'function'){
alert('The CallBackFun is not a function!');
}
if(cache){
pars += "&tmp="+Math.random();
}
createXMLRequestE();
xmlHttp2.onreadystatechange = function(){
if(xmlHttp2.readyState==4)
{
if(xmlHttp2.status==200)
{
callbackfun(xmlHttp2);
xmlHttp2.abort();
}
else
{
xmlHttp2.abort();
alert("请求出错!");
}
}
}
xmlHttp2.open(method,url+'?'+pars,true);
xmlHttp2.send(null);
}逻辑代码
function On_ClickButton() //出价按钮
{
var text=$get("TextPrice").value; //从文本框里获取价格
var myReg=new RegExp("^[0-9]*$");
if(myReg.test(text)) //验证输入的价格是否合法
{
var name="李四";
var pid=$get("hid").value;
startRequest('get','User.ashx','pid='+pid+'&name='+escape(name)+'&price='+text+'&time='+new Date().getTime()+'',ShowItUser,true); //传输商品id,竞拍人,和出的价格,然后判断的价格是否低于目前最高价然后返回
}
else
{
alert("您输入的格式有误,请重新输入!");
}
}
function ShowItUser(req)//用户返回
{
if(req.responseText=="no")
{
alert("您的余额不足20元,请充值后在参加竞拍!");
}
if(req.responseText=="noP")
{
alert("您出的价格不能低于起拍价!");
}
else //如果返回正确增加数据
{
startRequestE('get','Shuaxin.ashx','pid='+pid+'&name='+escape(name)+'&money='+text+'&timetext='+time+'&time='+new Date().getTime()+'',ShowIt,true);
}
}
1:我在User.ashx文件里判断出价不能低于目前最高价,在我写上价格以后,点出价按钮,一切正常,只要一秒钟内不连续点一切正常。如果在一秒钟内连续点3到4次出价按钮,过1秒到2秒以后,增加了3条到4条出价一样的数据。
问:这个问题服务器响应慢还是我的逻辑有问题,有没有更好的办法来解决这个问题?
看图:2:我这里用了2次ajax怎么整合成1个(我之前也试过可是全部用一个调用会出错)
3:希望大家对我写的代码多提下意见
ajax1的调用方如下: var xmlHttp1;
function createXMLRequest()
{
if(window.ActiveXObject)
{
xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp1=new XMLHttpRequest();
}
else
{
alert("不能创建XMLHttpRequest");
}
}
function startRequest(method,url,pars,callbackfun,cache)
{
if(typeof callbackfun != 'function'){
alert('The CallBackFun is not a function!');
}
if(cache){
pars += "&tmp="+Math.random();
}
createXMLRequest();
xmlHttp1.onreadystatechange = function(){
if(xmlHttp1.readyState==4)
{
if(xmlHttp1.status==200)
{
callbackfun(xmlHttp1);
xmlHttp1.abort();
}
else
{
xmlHttp1.abort();
alert("请求出错!");
}
}
}
xmlHttp1.open(method,url+'?'+pars,true);
xmlHttp1.send(null);
}
ajax1的调用方如下: var xmlHttp2;
function createXMLRequestE()
{
if(window.ActiveXObject)
{
xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp2=new XMLHttpRequest();
}
else
{
alert("不能创建XMLHttpRequest");
}
}
function startRequestE(method,url,pars,callbackfun,cache)
{
if(typeof callbackfun != 'function'){
alert('The CallBackFun is not a function!');
}
if(cache){
pars += "&tmp="+Math.random();
}
createXMLRequestE();
xmlHttp2.onreadystatechange = function(){
if(xmlHttp2.readyState==4)
{
if(xmlHttp2.status==200)
{
callbackfun(xmlHttp2);
xmlHttp2.abort();
}
else
{
xmlHttp2.abort();
alert("请求出错!");
}
}
}
xmlHttp2.open(method,url+'?'+pars,true);
xmlHttp2.send(null);
}逻辑代码
function On_ClickButton() //出价按钮
{
var text=$get("TextPrice").value; //从文本框里获取价格
var myReg=new RegExp("^[0-9]*$");
if(myReg.test(text)) //验证输入的价格是否合法
{
var name="李四";
var pid=$get("hid").value;
startRequest('get','User.ashx','pid='+pid+'&name='+escape(name)+'&price='+text+'&time='+new Date().getTime()+'',ShowItUser,true); //传输商品id,竞拍人,和出的价格,然后判断的价格是否低于目前最高价然后返回
}
else
{
alert("您输入的格式有误,请重新输入!");
}
}
function ShowItUser(req)//用户返回
{
if(req.responseText=="no")
{
alert("您的余额不足20元,请充值后在参加竞拍!");
}
if(req.responseText=="noP")
{
alert("您出的价格不能低于起拍价!");
}
else //如果返回正确增加数据
{
startRequestE('get','Shuaxin.ashx','pid='+pid+'&name='+escape(name)+'&money='+text+'&timetext='+time+'&time='+new Date().getTime()+'',ShowIt,true);
}
}
1:我在User.ashx文件里判断出价不能低于目前最高价,在我写上价格以后,点出价按钮,一切正常,只要一秒钟内不连续点一切正常。如果在一秒钟内连续点3到4次出价按钮,过1秒到2秒以后,增加了3条到4条出价一样的数据。
问:这个问题服务器响应慢还是我的逻辑有问题,有没有更好的办法来解决这个问题?
看图:2:我这里用了2次ajax怎么整合成1个(我之前也试过可是全部用一个调用会出错)
3:希望大家对我写的代码多提下意见
function On_ClickButton() //出价按钮
{
var text=$get("TextPrice").value; //从文本框里获取价格
var myReg=new RegExp("^[0-9]*$");
if(myReg.test(text)) //验证输入的价格是否合法
{
var name="李四";
var pid=$get("hid").value;
startRequest('get','User.ashx','pid='+pid+'&name='+escape(name)+'&price='+text+'&time='+new Date().getTime()+'',ShowItUser,true); //传输商品id,竞拍人,和出的价格,然后判断的价格是否低于目前最高价然后返回
}
else
{
alert("您输入的格式有误,请重新输入!");
}
}
function ShowItUser(req)//用户返回
{
if(req.responseText=="no")
{
alert("您的余额不足20元,请充值后在参加竞拍!");
}
if(req.responseText=="noP")
{
alert("您出的价格不能低于起拍价!");
}
else //如果返回正确增加数据
{
startRequestE('get','Shuaxin.ashx','pid='+pid+'&name='+escape(name)+'&money='+text+'&timetext='+time+'&time='+new Date().getTime()+'',ShowIt,true);
}
}
返回成功后 下面new Date().getTime() 不要在用这个了
用点击前 startRequest('get','User.ashx','pid='+pid+'&name='+escape(name)+'&price='+text+'&time='+new Date().getTime()+'',ShowItUser,true); //传输商品id,竞拍人,和出的价格,然后判断的价格是否低于目前最高价然后返回
这里面的时间
因为 用户点击的时候 成功了就用点击时候的时间 不要用返回正常后 的时间