【问题标题】:Simple character frequency counter, but why won't the count happen in realtime?简单的字符频率计数器,但为什么不会实时计数?
【发布时间】:2013-07-18 20:13:25
【问题描述】:

作为一个小项目,我正在尝试创建一个简单的在线血液学差异计数器。物理计数器(只不过是美化的计算器)运行的实验室每台设备的价格超过 1000 美元。不发达国家的小型实验室和实验室无法负担这笔费用,因此在线解决方案将很有价值。

HTML

<h1>Use the number pad to count cells</h1>
  <textarea id="textarea"></textarea>
<br>Cells counted:
<br>
<input type=text id="counter" size=5><br>
Blasts:<br><input type=text id="bl_num" size=5><br>
Blasts%:<br><input type=text id="bl_pct" size=5><br>
Segmented and band neutrophils:<br><input type=text id="segs_num" size=5><br>
Segmented and band neutrophils%:<br><input type=text id="segs_pct" size=5><br>
Lymphocytes:<br><input type=text id="lymph_num" size=5><br>
Lymphocytes%:<br><input type=text id="lymph_pct" size=5><br>
Metamyelocytes/myelocytes:<br><input type=text id="meta_num" size=5><br>
Metamyelocytes/myelocytes%:<br><input type=text id="meta_pct" size=5><br>
Promyelocytes:<br><input type=text id="pro_num" size=5><br>
Promyelocytes%:<br><input type=text id="pro_pct" size=5><br>
Monocytes:<br><input type=text id="mono_num" size=5><br>
Monocytes%:<br><input type=text id="mono_pct" size=5><br>
Nrbc:<br><input type=text id="nrbc_num" size=5><br>
Nrbc%:<br><input type=text id="nrbc_pct" size=5><br>
Eosinophils:<br><input type=text id="eos_num" size=5><br>
Eosinophils%:<br><input type=text id="eos_pct" size=5><br>
Plasma cells:<br><input type=text id="plasma_num" size=5><br>
Plasma cells%:<br><input type=text id="plasma_pct" size=5><br>
Basophils:<br><input type=text id="baso_num" size=5><br>
Basophils%:<br><input type=text id="baso_pct" size=5><br>
Atypical cells:<br><input type=text id="atyp_num" size=5><br>
Atypical cells%:<br><input type=text id="atyp_pct" size=5><br>

我的脚本:

$(window).load(function(){
$("#textarea").keyup(function () {
    var box = $(this).val();
    var len = box.length;
    if (len < 201) {
        $('#counter').val(len);
// count blasts
    var _blast = box.match(/8/g).length;
    var _blast_pct = (_blast / len) * 100;
    $('#bl_num').val(_blast);
    $('#bl_pct').val(_blast_pct);
    // count segs
    var _segs  = box.match(/3/g).length;
    var _segs_pct = (_segs / len) * 100;
    $('#segs_num').val(_segs);
    $('#segs_pct').val(_segs_pct);
    // count meta/myelo
    var _meta  = box.match(/7/g).length;
    var _meta_pct = (_meta / len) * 100;
    $('#meta_num').val(_meta);
    $('#meta_pct').val(_meta_pct);
    // count pros
    var _pro  = box.match(/5/g).length;
    var _pro_pct = (_pro / len) * 100;
    $('#pro_num').val(_pro);
    $('#pro_pct').val(_pro_pct);
    // count lymphs
    var _lym  = box.match(/2/g).length;
    var _lym_pct = (_lym / len) * 100;
    $('#lymph_num').val(_lym);
    $('#lymph_pct').val(_lym_pct);
    // count nrbcs
    var _nrbc  = box.match(/9/g).length;
    var _nrbc_pct = (_nrbc / len) * 100;
    $('#nrbc_num').val(_nrbc);
    $('#nrbc_pct').val(_nrbc_pct);
    // count eos
    var _eos  = box.match(/4/g).length;
    var _eos_pct = (_eos / len) * 100;
    $('#eos_num').val(_eos);
    $('#eos_pct').val(_eos_pct);
    // count mono
    var _mono  = box.match(/1/g).length;
    var _mono_pct = (_mono / len) * 100;
    $('#mono_num').val(_mono);
    $('#mono_pct').val(_mono_pct);
    // count plasma
    var _plasma  = box.match(/6/g).length;
    var _plasma_pct = (_plasma / len) * 100;
    $('#plasma_num').val(_plasma);
    $('#plasma_pct').val(_plasma_pct);
    // count basos
    var _baso  = box.match(/\+/g).length;
    var _baso_pct = (_baso / len) * 100;
    $('#baso_num').val(_baso);
    $('#baso_pct').val(_baso_pct);
    // count atypcal cells
    var _atyp  = box.match(/\-/g).length;
    var _atyp_pct = (_atyp / len) * 100;
    $('#atyp_num').val(_atyp);
    $('#atyp_pct').val(_atyp_pct);
    } else {
        alert("You have counted 200 cells");
    }

});
}); 

我有这个粗略的例子工作(见这里:http://jsfiddle.net/xpMTE/),但我不明白为什么字符频率数据不能实时填充?当用户使用数字键盘对他们在显微镜中看到的细胞进行计数时,计数器似乎会先等待某些情况发生,然后再填充图表的其余部分。这不是一个理想的条件。

【问题讨论】:

    标签: javascript jquery character counter onkeyup


    【解决方案1】:

    好吧,我想我明白了。我的控制台日志突出显示了一个错误,如果我的 .match 语句没有找到任何匹配项,它将返回 null,因此 null.length 无效。

    因此,我在初始 if 语句之外使用了 .match 参数,而是添加了一系列 if 语句,例如:

        // count blasts
    if (box.match(/8/g) !== null) {
        var _blast = box.match(/8/g).length;
        var _blast_pct = (_blast / len) * 100;
        $('#bl_num').val(_blast);
        $('#bl_pct').val(_blast_pct);
    }
    

    每次按键匹配都以此类推。

    现在,这些框会实时填充。

    请看这里:http://jsfiddle.net/xpMTE/1/

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 2017-10-01
      • 2023-02-21
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多