Source Code

for file /AJAXEngine/S02_AJAXCoreSamples/CalcFactorsService.htm

<!DOCTYPE html>
<html lang="en">
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Prime factors calculator using WebServices for JavaScript</title>
  <script type="text/javascript" src="/ajaxcore/ajax.js"></script>
  <script type="text/javascript" src="/ajaxcore/GetJavaScriptProxy.aspx?service=/AJAXEngine/S02_AJAXCoreSamples/CalcService.asmx"></script>

  <h1>Prime factors calculator using WebServices for JavaScript</h1>
        <th><label for="inputField">Please enter a number:</label></th>
          <input id="inputField" onkeyup="StartCalcPrimeFactors()">
        <td style="padding-left: 12px">
          <button onclick="proxies.EnableCache(proxies.CalcService.CalcPrimeFactors)">enable caching</button>
        <th><label>The factors are:</label></th>
          <input id="outputField" size="60" disabled="disabled">
  <p>try 12313123123123 or 12313123123123123123 for long running calculations ! </p>
  <script defer="defer" type="text/javascript">

    // to set up a debug output in an alert box use:
    // proxies.CalcService.CalcPrimeFactors.corefunc = proxies.alertResult;

    // to set up a debug output of the http response body in an alert box use:
    // proxies.CalcService.CalcPrimeFactors.corefunc = proxies.alertResponseText;

    // attach window.alert for displaying result to make the direct webservice call asynchronous
    // proxies.CalcService.CalcPrimeFactors.func = window.alert;

    // alert any exceptions...
    // proxies.CalcService.CalcPrimeFactors.onException = proxies.alertException;

    var _num = "";
    var _timer = null;

    function StartCalcPrimeFactors() {
      var inputText = document.getElementById("inputField").value;
      if (_num != inputText) {
        if (_timer != null)
        document.getElementById("outputField").value = "wait...";
        _num = inputText;
        // wait 300 msec. before continuing. This allows further typing...
        _timer = window.setTimeout(CalcPrimeFactors, 300, "javascript");
      } // if

    // calc prime factors
    function CalcPrimeFactors() {
      var inputText = document.getElementById("inputField").value;

      if ((inputText == null) || (inputText.length == 0) || (inputText == "0"))
        return; // need no calculation

      if (proxies.IsActive()) {
        // try again later...
        _timer = window.setTimeout(CalcPrimeFactors, 300, "javascript");
      } // if

      proxies.CalcService.CalcPrimeFactors.func = displayFactors;  // hook up a method that gets the response
      proxies.CalcService.CalcPrimeFactors(inputText); // now call the server
    } // CalcPrimeFactors

    // The return value is passed to this function as a parameter
    function displayFactors(retVal) {
      document.getElementById("outputField").value = retVal;
    } // displayFactors</pre>

  <hr />
    This sample uses the WebServices for JavaScript on the client to call a WebService that calculates
    the prime numbers of a given number.
    This page is part of the <a href=""></a>
  <hr />


This page is part of the web site.

For updates and discussions see