如何将值传递给javascript函数的参数并获得结果

时间:2015-08-12 13:02:16

标签: javascript java jsp

我有一个javascript函数,它是计算呼叫中心所需代理数量的公式。它接受5个值并返回一个值,即所需代理的数量 任何人都可以通过传递几个值并获得此特定函数的输出来显示此javascript的示例。我想在jsp页面中使用。

JS:

function Calculate( arrivalRate, callDuration, serviceLevelP, serviceLevelT, agentsCount ) {
    var rate = arrivalRate / 3600;
    var u = rate * callDuration;
    var sl = 0;
    var SL = serviceLevelP / 100;
    var m = Math.floor( u );
    var ummf = Math.pow( u, m ) / fact( m );
    var sm = 0;
    var ukkf = 1;
    for ( var i = 0;
    i < m;
    ++ i ) {
        ukkf = Math.pow( u, i ) / fact( i );
        sm += ukkf;
    }
    var tTs = serviceLevelT / callDuration;
    do {
        m ++;
        ummf *= u / m;
        ukkf *= u / ( m - 1 );
        sm += ukkf;
        var p = u / m;
        var E = ummf / ( ummf + ( 1 - p ) * sm );
        sl = 1 - E * Math.exp( ( u - m ) * tTs );
    }
    while ( sl < SL );
    agentsCount.SetValue( m );
    function fact( x ) {
        var ret = 1;
        for ( var i = 2;
        i <= x;
        ++ i ) {
            ret *= i;
        }
        return ret;
    }
}

1 个答案:

答案 0 :(得分:3)

非常一般示例 - 您不清楚如何哪里从中获取值:

<%@ page language="java" %>
...
<html>
  ...
  // somewhere in the <head> a <script> tag with your function
  ...
  <c:set var="jspArrivalRate" value="123" />
  <c:set var="jspCallDuration" value="456" />
  <c:set var="jspServiceLevelP" value="789" />
  <c:set var="jspServiceLevelT" value="1011" />
  <c:set var="jspAgentsCount" value="12" />
  ...
  <script>
    var result = Calculate(${jspArrivalRate}, ${jspCallDuration}, ${jspServiceLevelP}, ${jspServiceLevelT}, ${jspAgentsCount});
    // do whatever you want with the result
  </script>
  ...
</html>