new-functs
v1.1.2
Published
A library that introduces a bunch of new functions.
Downloads
16
Readme
New-Functs
A library that introduces a bunch of new functions.
Documentation:
nf
- The function that holds all the other functions in its object that's returned from calling the function.
nf.pushMaterial(arrayToPushInto, whatToPush, starting, ending, addition)
EXAMPLE 1:
let array = [];
nf.pushMaterial(array, nf.pM_i, 0, 10, 1);
//(By the way, nf.pM_i is an optional variable that is used whenever you're using nf.pushMaterial and it just means the iteration count of the for loop. This is how you can create repeating numbers, like shown in the output below. The addition parameter will change how much nf.pM_i changes for each loop. So, if the addition parameter is 2, it would output "0, 2, 4, 6, 8, 10, 12, 14, 16, 18")
console.log(array); //Outputs "0, 1, 2, 3, 4, 5, 6, 7, 8, 9"
EXAMPLE 2:
let array = [];
let a = 0;
nf.pushMaterial(array, a, 0, 10, 1);
console.log(array); //Outputs "0, 0, 0, 0, 0, 0, 0, 0, 0, 0"
nf.timer
Variable:
IMPORTANT: You will have to put the function nf.timer.update();
in the draw function for the timer variable to work.
There is a variable inside the object that is returned from nf
called timer
. This holds the Timer constructor.
There are functions followed with it, such as:
nf.timer.timeout(callback, delay, repetitions);
- This will execute the callback function in the callback parameter after the amount of milliseconds that are passed in the delay parameter have passed. The repetition parameter will decide how many times it will repeat this.
EXAMPLE 1:
function draw() { nf.timer.update(); }; nf.timer.timeout(function() { console.log("Hello, World!"); }, 1000, 3); //Outputs "Hello, World!" 3 times, each after 1000 milliseconds (1 second)
nf.timer.interval(callback, delay); & nf.timer.removeInterval(id);
nf.timer.interval will execute the callback function in the callback parameter after the amount of milliseconds that are passed in the delay parameter have passed repeatedly.
Once you call
nf.timer.removeInterval();
with the id (variable) of the interval, then you will remove the interval and stop it completely. (See example below)
EXAMPLE 1:
// Create a function to be called repeatedly let printMessage = function() { console.log("Hello, World!"); }; // Start an interval that calls printMessage every second (1000 milliseconds) let myIntervalID = nf.timer.interval(printMessage, 1000); function draw() { nf.timer.update(); // Let's say we want to clear the interval after 5 seconds (5000 milliseconds) // We'll use the "nf.timer.timeout();" function for this nf.timer.timeout(function() { console.log("Interval cleared!"); nf.timer.removeInterval(myIntervalID); }, 5000); };
nf.timer.pause(id, type); & nf.timer.resume(id, type);
- This will pause/resume an interval or a timeout. If you use
nf.timer.pause();
and give thetype
parameter the value of"interval"
then it will pause the interval that has the id of the id you give in the id parameter. Same thing if you set thetype
parameter to"timeout"
. Makes more sense if you see the examples.
EXAMPLE 1 WITH INTERVAL:
// initialize the counter let counter = 0; // this function increments the counter and prints it let count = function() { counter++; console.log("Counter: " + counter); }; // setting up an interval that will call count function every second let intervalID = nf.timer.interval(count, 1000); // setting up a timeout that will pause the interval after 5 seconds nf.timer.timeout(function() { console.log("Pausing interval..."); nf.timer.pause(intervalID, 'interval'); }, 5000, 1); // setting up a timeout that will resume the interval after 5 more seconds nf.timer.timeout(function() { console.log("Resuming interval..."); nf.timer.resume(intervalID, 'interval'); }, 10000, 1); function draw() { nf.timer.update(); };
EXAMPLE 2 WITH TIMEOUT:
// initialize the counter let counter = 0; // this function increments the counter and prints it let incrementCounter = function() { counter++; console.log("Counter: " + counter); }; // setting up a timeout that will call incrementCounter function after 1 seconds let timeoutID = nf.timer.timeout(incrementCounter, 1000, 2); // setting up a timeout that will pause the previous timeout 1.5 seconds after the program starts nf.timer.timeout(function() { console.log("Pausing timeout..."); nf.timer.pause(timeoutID, 'timeout'); }, 1500, 1); // setting up a timeout that will resume the paused timeout after 2 more seconds nf.timer.timeout(function() { console.log("Resuming timeout..."); nf.timer.resume(timeoutID, 'timeout'); }, 3500, 1); function draw() { nf.timer.update(); };
- This will pause/resume an interval or a timeout. If you use