NetTalk Central

Author Topic: Starting Clarion proc from javascript  (Read 4727 times)

oggy

  • Full Member
  • ***
  • Posts: 219
    • View Profile
    • Email
Starting Clarion proc from javascript
« on: July 14, 2020, 12:34:53 PM »
In javascript, i need to do some clarion code
I set a timer for the webform,  and now on timer must do clarion code or call simple clarion functin...

<script>
var interval = 500; // ms, primjer: 1000 ms = 1 sekunda
var expected = Date.now() + interval;
setTimeout(step, interval);
function step() {
    var dt = Date.now() - expected;
    if (dt > interval) {
    }
do someroutine  // POINT WHERE I WANT TO START SOME CLARION CODE
    expected += interval;
    setTimeout(step, Math.max(0, interval - dt));
}
</script>

How?
Regards.

bshields

  • Sr. Member
  • ****
  • Posts: 392
    • View Profile
    • Inhabit
    • Email
Re: Starting Clarion proc from javascript
« Reply #1 on: July 14, 2020, 05:26:47 PM »
Hi,

Create a NetWebPage with a contentType of 'application/json'. We normally use json to talk to JS.

Its usually nice to give it a page name of 'YourProc.json' or similar so JS has a better idea of what its dealing with.

There are a bunch of ways of calling a server side function from JS. I often use .get or .ajax which are part of jQuery.

$.get('YourFunc.json?YoucanpassURLparamaters=1',function(data){
  if (data.somevariable==1) {
    // You can add some JS code in here based on the response
  };
});


Get is the simplest.

In clarion:

Packet.Append('{{"somevariable":1}')

Will allow you to read this variable in JS (as shown above).

Regards
Bill

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Starting Clarion proc from javascript
« Reply #2 on: July 14, 2020, 06:27:32 PM »
Hi Oggy,

In JavaScript you don't need the fancy interval check thing like you do on windows. So your code can be a lot simpler;

setTimeout(function{
            // ajaxGet etc
           },500)


NetTalk comes with a function that makes this a bit easier. it's called SetTimer.

So your JavaScript can look like this;

<script>
SetTimer('ProcedureName',500,'somename=value','othername=value');
</script>

Where ProcedureName is a procedure to call in the app (*), 500 is the time period (in milliseconds), param1 and param2 are optional parameters.

note that JavaScript is case sensitive, so be sure to get the word SetTimer right.

(*)
On the server side you make a procedure with the same name. This could be a NetWebPage or a NetWebServiceMethod. The reply should be XML (because the reply will be processed for you by the SetTimer)

Of course this will trigger a timer every 500 milliseconds. Over and over again. If you have 10 users on the page you will get a 10 requests from the page every second. So in general timers like this are not scalable. So while all the information is technically correct, you may find that a) 500ms is waaay too fast and b) using timers in the first place is a _really_ bad idea. Perhaps there are other ways to achieve what you are really wanting to do...


cheers
Bruce

oggy

  • Full Member
  • ***
  • Posts: 219
    • View Profile
    • Email
Re: Starting Clarion proc from javascript
« Reply #3 on: July 14, 2020, 11:16:57 PM »
Thanks Bill and Bruce. I just started to learn javascript, this is the code I found deep in internet ocean.
Thanks for advice.
Going to try to.
Of course, I know that the timer event is not smooth as some other event, this is just first step to get what I want.
I succeed to get some variable right from the javascript (this is all about one of my previous post), so now will try to do some action when variables change.

Thanks...
« Last Edit: July 14, 2020, 11:24:01 PM by oggy »

oggy

  • Full Member
  • ***
  • Posts: 219
    • View Profile
    • Email
Re: Starting Clarion proc from javascript
« Reply #4 on: July 15, 2020, 04:09:31 AM »
Hi,Bill.
couple of questions, of course I had it ,
$.get('YourFunc.json?YoucanpassURLparamaters=1',function(data){


On this part, where in getting function I can read that parameter, and second question, sending more than one parameter is possible?
EDIT BY STUPID ME: never mind, got it...
« Last Edit: July 15, 2020, 04:16:20 AM by oggy »