# Referencing functions in Javascript and setup

**URL:** <https://community.particle.io/t/referencing-functions-in-javascript-and-setup/15378>\
**Category:** Getting Started\
**Created:** [September 10, 2015, 8:02pm UTC](https://community.particle.io/t/referencing-functions-in-javascript-and-setup/15378 "2015-09-10T20:02:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtarlo](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jtarlo](https://community.particle.io/u/jtarlo)\
**Post date:** [September 10, 2015, 8:02pm UTC](https://community.particle.io/t/referencing-functions-in-javascript-and-setup/15378/1 "2015-09-10T20:02:51Z")

</div>

Hey, I need to reference some functions on the core by using javascript, and don’t know how to do so. I have been able do it through regular html, however i believe it is different. My code for the setup of the functions is below, as well as an example of the actual function. Could someone make an example of what the code should look like in javascript?

_Function setup:_

```
void setup() {
    Spark.function("motor1drive", motor1);
    Spark.function("motor1stop", motor1);
    pinMode(motor1, OUTPUT);
    
    Spark.function("motor2drive", motor2);
    Spark.function("motor2stop", motor2);
    pinMode(motor2, OUTPUT);
}

```

_Function example_

```
int motor2(String command) {
    if(command==motor2drive)
        loop() {
            analogWrite(motor,255); 
            delay(ms);
            analogWrite(motor,0);
            delay(ms);
        }
    }
    else if(command==motor2stop) {
        loop() {
            analogWrite(motor,0);  
            delay(ms);
            analogWrite(motor,0);
            delay(ms);
        }
    }
}

```

Also, how would you setup javascript in order to reference the core in general? thanks.

---

<div class="post-metadata">

**Author:** ![johnventions](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/johnventions/32/6622_2.png) [@johnventions](https://community.particle.io/u/johnventions)\
**Post date:** [September 10, 2015, 8:48pm UTC](https://community.particle.io/t/referencing-functions-in-javascript-and-setup/15378/2 "2015-09-10T20:48:29Z")

</div>

I think you can interact with them via a javascript ajax request to [https://api.particle.io/v1/devices/{deviceID}/{function-name}](https://api.particle.io/v1/devices/%7BdeviceID%7D/%7Bfunction-name%7D) with POST variables for the accessToken and any arguments for the function.  
See [https://docs.particle.io/reference/api/#call-a-function](https://docs.particle.io/reference/api/#call-a-function) for calling a function over the cloud.

On the HTML page, you’d want something like this…

```
<div id='myDiv'> Response of the function will go here </div>
<script>
    var accessToken = 1234;
    var deviceID = 5678;
    var baseURL = "https://api.particle.io/v1/devices/"+ deviceID + "/";
    function callSpark(fName, args) {
            var xmlhttp;
            if (window.XMLHttpRequest) {
                 xmlhttp=new XMLHttpRequest();
            } else {
                  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function() {
                      if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                               document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                      }
          }
          var fullURL = baseURL + fName;
          xmlhttp.open("POST", fullURL ,true);
          xmlhttp.send("access_token="+ accessToken + "&args=" + args);
    }
</script>

```

And then you would just call something like: callSpark(“motor1drive”, “command”) in javascript.  
Please excuse any errors (this is handwritten, and mostly theory)

But since your access code is in there, I wouldn’t recommend making that page public.  
I think @bko wrote a tutorial about using javascript to call a function on your spark…

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [September 10, 2015, 9:41pm UTC](https://community.particle.io/t/referencing-functions-in-javascript-and-setup/15378/3 "2015-09-10T21:41:32Z")

</div>

Check this out:

> [@Tutorial: Spark Variable and Function on One Web Page](https://community.particle.io/t/tutorial-spark-variable-and-function-on-one-web-page/4181):
>
> Let’s say you have a Spark core with both a Spark variable and a Spark function. In my case, I have the world’s simplest servo control sketch, using the nice miniature servo in the Spark Maker Kit. Using these parts, we will build a servo that you can control from a web page anywhere and monitor the position of the servo too! Here’s a preview of the finished tutorial: [image] There is a coarse adjust slider, two fine adjust buttons, and the current position of the servo is displayed. That…

I've written an example over here, using the JavaScript SDK, just right-click to check the source: [http://jordymoors.nl/particle/demos/function\_button.html](http://jordymoors.nl/particle/demos/function_button.html)
