Sending a bang with Javascript

Paul Quirie's icon

Hi,
I'm trying to trigger a js object with a bang then send a bang for each even number in the array using the below function, but getting 'error calling function bang'.
Can anyone advise what I'm missing please?

inlets = 2;
outlets = 1;

function bang() {
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 == 0) {
post(numbers[i]);
outlet(0, "bang");
}
}

post();
}

pdelges's icon

I pasted your code into a [js] object (Max831 under OSX X.14.6) and it does work as expected. No error.

Paul Quirie's icon

Thanks for checking, I'm running the same versions but still getting the same error.
At least the codes right, is the outlet connected to a button?
Cheers

tyler mazaika's icon

Are you certain that the js file you are reading/ using is the only one with same name? and in your search path? And that you have compiled your current changes?

Paul Quirie's icon

Yes, it's the same file, updating with VSCode but the changes are coming through to the file in Max.
I've got it working now with this -
function bang(bang) {
outlet(0, "bang");
}
With a button patched on the inlet through the js file that triggers the button patched on the outlet.
Just having issues now trying to trigger with a for loop. Getting the same 'no function bang' error when trying to trigger on conditions.

Paul Quirie's icon

It's working now cheers...Is there any kind of timeout function for the js max objects?

tyler mazaika's icon

1) what’s your proof changes are coming through? post() statements on compilation/load of the JS file? It doesn’t look like you have autowatch=1 set in your JS. Just suspicious because when using an external editor, double clicking the JS object box opens / reveals the file in the editor, not what Max had compiled.

2) im very nervous about a function named bang which uses an argument also named bang.

3) your initial for loop code looks totally fine to me, which is why I’m remaining suspicious of a configuration/compilation issue.

I really wish Max would produce meaningful errors and not swallow then into “error calling function <foo>”. At the most extreme form I’ve had to do line by line post() messages just to see what line caused fai.

tyler mazaika's icon

ok, nice.

You can use the Task() object to poll and manage a timeout.

Paul Quirie's icon

I'm running -
function bang() {
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 == 0) {
post(numbers[i]);
outlet(0, "bang");
}
}
post();
}

The even numbers are printing to the console now with no errors. I'm not sure that I'm getting a bang on each iteration though yet if that's even possible. Thinking I can use Task() to pause each iteration ?
I've added autowatch 1 now so that should help with any compiling issues.
The errors are not helpful at all, a line by line post() that's a great idea.