Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with setTimeout in javascript for MaxMSP

Tags:

javascript

I've already looked at quite a few other questions but I can't seem to fix this issue with setTimeout

So I've been working on this and came to this, but for some reason the setTimeout does not work, any tips?

function curves(val_name, mini, maxi, t_amount, steps) {
    //t_amount MUST be in ms
    for (x = 0; x < steps; x++) {
        var x_mod = scale(x, -6, 0, 0, steps);
        var value = setTimeout(calculate_curve, (t_amount / steps), x_mod);

        switch (val_name) {
            case "vol_stretch1":
                var vol_stretch1 = this.patcher.getnamed("stretching").subpatcher(0).getnamed("vol_stretch1");
                vol_stretch1 = value
                break;
            case "vol_stretch2":
                var vol_stretch2 = this.patcher.getnamed("stretching").subpatcher(0).getnamed("vol_stretch2");
                vol_stretch2 = value
                break;
            case "vol_stretch3":
                var vol_stretch3 = this.patcher.getnamed("stretching").subpatcher(0).getnamed("vol_stretch3");
                vol_stretch3 = value
                break;
        }
    }
}

function calculate_curve(x) {
    var constant_e = 2.718281828459;
    var result = (1 / 1 + (constant_e ^ (x * -1))) * -1; //sigmoid function * -1 to have the nice rise
}

function scale(unscaledNum, minAllowed, maxAllowed, minimum, maximum) {
    return (maxAllowed - minAllowed) * (unscaledNum - minimum) / (maximum - minimum) + minAllowed;
}

You can ignore the switch as it works with an extension for MaxMSP but isn't very important here. The error I get back is "Javascript ReferenceError: setTimeout is not defined". Any help is greatly appreciated!

like image 881
maladie Avatar asked May 18 '26 14:05

maladie


1 Answers

I've not worked with Max before, but from a small amount of searching it looks like you're writing something along the lines of a plugin.

It looks like Max is running it's own Javascript environment of some sort. setTimeout is a method on the window object of browsers in Javascript, and as such it is not necessarily implemented in Javascript outside of the browser, as Max appears to be.

The recommended alternative seems to be to use the Task object exposed by the environment, which has some documentation here: https://docs.cycling74.com/max5/vignettes/js/jstaskobject.html

I have no way of testing this, but from the documentation it looks like something along the lines of the below should work:

var task = new Task(function() {
    calculate_curve(x_mod);
}, this);
task.schedule((t_amount / steps));
like image 169
Hecksa Avatar answered May 21 '26 04:05

Hecksa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!