Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 存在实时计算问题,计算每个关键冲程_Javascript_Jquery_Html - Fatal编程技术网

Javascript 存在实时计算问题,计算每个关键冲程

Javascript 存在实时计算问题,计算每个关键冲程,javascript,jquery,html,Javascript,Jquery,Html,我有一个表,它根据用户类型的输入计算总数。我的问题是jquery代码计算每个键的笔划,而不是在您停止键入后“获取”整个数字。代码如下,如有任何帮助,将不胜感激 $(document).ready(function() { $('input.refreshButton').bind('click', EstimateTotal); $('input.seatNumber').bind('keypress', EstimateTotal); $('input.seatNumber').bind('c

我有一个表,它根据用户类型的输入计算总数。我的问题是jquery代码计算每个键的笔划,而不是在您停止键入后“获取”整个数字。代码如下,如有任何帮助,将不胜感激

$(document).ready(function() {
$('input.refreshButton').bind('click', EstimateTotal);
$('input.seatNumber').bind('keypress', EstimateTotal);
$('input.seatNumber').bind('change', EstimateTotal);
}))

/$('input[type=submit]')。live('click',function(){
函数估计总计(事件){
var tierSelected=$(this.attr('data-year');
var numberSeats=Math.floor($('#numberSeats'+tierSelected.val());
$('.alertbox_error_'+tierrelected).hide();
如果(isNaN(NumberSets)| | NumberSets==0){
$('.alertbox_error_'+已选择分层)。show();
}否则{
$('.alertbox_error_'+tierrelected).hide();
var=0;
var seatLow=0;
var-base=0;
var yearTotal=0;
var totalsArray=[];
var currentYear=0;
$('.tier_'+tierSelected)。每个(函数(){
seatLow=$(this.attr('data-seat_-low');
firstSeatLow=$(this.attr('data-first_-seat_-low');
seahight=$(this.attr('data-seat_-high');
座椅底座=$(this).attr(‘数据库成本’);
costPerSeat=$(this.attr(‘每个座位的数据成本’);
年份=$(this.attr('data-year');
座位=0;
如果(年!=当前年){
如果(当前年份>0){
totalsArray[当前年份]=年度总计;
}
当前年份=年;
年份总数=0;
}
如果(数字测试>=SEAHTH){
座椅=数学地板(座椅大腿-座椅下面+1);
}否则如果(numberSets>=seatLow){
座椅=数学楼层(数字座椅-座椅下方+1);
}
如果(座位<0){
座位=0;
}
年总数+=数学地板(成本价格)*数学地板(座位)*数学地板(年)+数学地板(座椅座);
});
totalsArray[当前年份]=年度总计;
totalsArray.forEach(函数(项、键){
如果(项目>1000000){
$('.totalCost\'+tierSelected+'[数据年=“'+key+'']”)。追加('Contact Us');
}否则{
$('.totalCost_'+tierSelected+'[data year=“'+key+'']”)追加('$'+item);
}
});
}
}

您需要一个
设置超时
,以及一种通过按键消除/重置超时的方法

我个人会这样做:

var calc_delay;
$(document).ready(function() {
$('input.refreshButton').bind('click', runEstimateTotal);
$('input.seatNumber').bind('keypress', runEstimateTotal);
$('input.seatNumber').bind('change', runEstimateTotal);
});

function runEstimateTotal(){
    clearTimeout(calc_delay);
    calc_delay = setTimeout(function(){ EstimateTotal(); }, 100);
}

function EstimateTotal() {
    ....

这会提示系统在每次按键后计算100ms,除非检测到另一个事件(即调用runEstimateTotal),在这种情况下,延迟倒计时将重置。

尝试将计算函数中的所有内容包装在setTimeout中(function(){//code here},100),100毫秒或更短。这将给函数一秒钟的计算时间,用户不会看到任何延迟,这取决于您的时间设置。请注意,如果您确实需要使用事件数据,则需要详细说明/改进这一点-设置方式可能有点棘手。但对于您提供的函数,这应该足够了。t谢谢你的输入。我试着添加了这个函数,但没有成功。我做了一些小的修改,现在它计算正确了。主要的修改是将keypress事件更改为keyup。
var calc_delay;
$(document).ready(function() {
$('input.refreshButton').bind('click', runEstimateTotal);
$('input.seatNumber').bind('keypress', runEstimateTotal);
$('input.seatNumber').bind('change', runEstimateTotal);
});

function runEstimateTotal(){
    clearTimeout(calc_delay);
    calc_delay = setTimeout(function(){ EstimateTotal(); }, 100);
}

function EstimateTotal() {
    ....