ReferenceError: y is not defined

y is not defined, said the error, with a creepy smile on her face...


If y (at the end of the script) is replaced by a number, the error goes away...

script
var diff = 50
var q = ~~(diff/50)
var r = diff % 50
var aa = 2 // +50 bars ms/iteration
var bb = 1 // +10
var cc = 0.5 // +1

/////////////////////////////////////////////////////////////////////
if (q > 0) {

// 1st block
for (var i=0; i<q; i++) {
	setTimeout(i, function(){
		send('midi:SessionKiano','/note',7,40,1) // +50 (repeat q)
	}, i * aa)
}

// 2nd block (delayed)
//////////////////////
setTimeout(function(){
	if (r > 10){
		q = ~~(r/10)
		r = r % 10
		for (var x=0; x<q; x++) {
			setTimeout(x, function(){
		send('midi:SessionKiano','/note',7,39,1) // +10 (repeat q)
	}, x * bb)
		}
		setTimeout(function(){
			for (var y=0; y<r; y++) {
				setTimeout(y, function(){
		send('midi:SessionKiano','/note',1,72,1) //+1 (repeat r)
	}, y * cc)
			}
		}, x * cc)
	}
else if (r < 10) {
	for (var y=0; y<r; y++) {
		setTimeout(y, function(){
			send('midi:SessionKiano','/note',1,72,1)
		}, y * cc)
	}
}
},i*aa+10) // delay of the 2nd block

}

/////////////////////////////////////////////////////////////////
if (q == 0) {

// 1st block 
////////////////
	if (r > 10){
		q = ~~(r/10)
		r = r % 10
		for (var x=0; x<q; x++) {
			setTimeout(x, function(){
		send('midi:SessionKiano','/note',7,39,1) // +10 (repeat q)
	}, x * bb)
		}

		// 2nd block (delayed)
		//////////////////////
		setTimeout(function(){
			for (var y=0; y<r; y++) {
				setTimeout(y, function(){
		send('midi:SessionKiano','/note',1,72,1) //+1 (repeat r)
	}, y * cc)
			}
		}, x * bb) // delay of the 2nd block

		setTimeout(function(){
// sets the used buttons to a 0 value state
set('push_43',0, {send: false}) 
set('Backward one bar', 0, {send: false})
set('forward_one_bar', 0, {send: false})

// starts the playback
send('midi:SessionKiano','/note',1,75,1)
}, x * bb + y * cc) // the problem is here!
	}
}

What I'm trying to do:

I'm trying to create a mechanism that locates the playhead in Cubase using different macros.

If I need to go to bar 449, I enter 449 and the script will:
∆ trigger the +50 bar macro a number of times equal to the quotent of 449 divided by 50., then
∆ trigger the +10 bar macro a number of times equal to another quotent (49 divided by 10), then
∆ trigger the +1 bar macro a number of times equal to the remainder of 49 divided by 10.

If it's too hard to find what the problem is, it's ok... Maybe I need to start over...

Thank you.

https://www.w3schools.com/js/js_scope.asp

y is indeed undefined in the last timeout block. Its defined in another block that doesn't contain that the block where you call y. I guess you could simply use r - 1 instead of y.

1 Like