I have a function to generate some random-ish numbers and inside that function I make a call to setInterval() because I need those numbers to refresh every 2 seconds.
function genSine(val1, val2) {
var freq = 0.1; // angular frequency
var coords = function(x, y) {
var amplitude = Math.floor((Math.random()*10)+1);
var phase = Math.floor((Math.random()*20)+1);
return {
x : Math.sin(freq * (val1 + phase) * amplitude) + Math.floor((Math.random()*100)+1),
y : Math.sin(freq * (val2 + phase) * amplitude) + Math.floor((Math.random()*100)+1)
};
}
setInterval(function() {
current = coords(50, 50);
console.log('Val 1: ' + current.x);
console.log('Val 2: ' + current.y);
}, 2000);
}
genSine(10, 20);
This is all well and good, the values update as expected but my goal is to have two global variables (let's call them val1/val2) update within that setInterval() function. It appears I have a scoping issue because those variables are not accessible within the function and I can not access the 'current' variable from outside that function. I know I am missing something here, what is it?
Fiddle: /
I have a function to generate some random-ish numbers and inside that function I make a call to setInterval() because I need those numbers to refresh every 2 seconds.
function genSine(val1, val2) {
var freq = 0.1; // angular frequency
var coords = function(x, y) {
var amplitude = Math.floor((Math.random()*10)+1);
var phase = Math.floor((Math.random()*20)+1);
return {
x : Math.sin(freq * (val1 + phase) * amplitude) + Math.floor((Math.random()*100)+1),
y : Math.sin(freq * (val2 + phase) * amplitude) + Math.floor((Math.random()*100)+1)
};
}
setInterval(function() {
current = coords(50, 50);
console.log('Val 1: ' + current.x);
console.log('Val 2: ' + current.y);
}, 2000);
}
genSine(10, 20);
This is all well and good, the values update as expected but my goal is to have two global variables (let's call them val1/val2) update within that setInterval() function. It appears I have a scoping issue because those variables are not accessible within the function and I can not access the 'current' variable from outside that function. I know I am missing something here, what is it?
Fiddle: http://jsfiddle/niczak/4uNen/1/
Share Improve this question edited Dec 20, 2013 at 14:47 user1636522 asked Nov 25, 2013 at 22:14 niczakniczak 3,89911 gold badges47 silver badges65 bronze badges 2-
2
declare
var current = {};
outside the function – Pedro Estrada Commented Nov 25, 2013 at 22:17 - here is a jsfiddle jsfiddle/4uNen/2 – Pedro Estrada Commented Nov 25, 2013 at 22:33
3 Answers
Reset to default 3You just need to define var current = {};
in the global scope, and be sure not to define var current
in any other scope. current=[whatever];
is fine, as long as there's no var
.
Edit: I'm not sure what you did, but I fixed your fiddle with this code:
var current;
function genSine(val1, val2) {
var freq = 0.1; // angular frequency
var coords = function(x, y) {
var amplitude = Math.floor((Math.random()*10)+10);
var phase = Math.floor((Math.random()*20)+10);
return {
x : Math.sin(freq * (val1 + phase) * amplitude) + Math.floor((Math.random()*100)+1),
y : Math.sin(freq * (val2 + phase) * amplitude) + Math.floor((Math.random()*100)+1)
};
}
setInterval(function() {
current = coords(50, 50);
console.log(current);
$(".display").html(JSON.stringify(current));
}, 500);
}
genSine(10, 20);
setInterval(function() {
$(".display2").html("d2:"+JSON.stringify(current));
}, 200);
Your current
is fine in that scope, it is your coords
function that is not in the right scope. Put that in the global scope together with the freq
variable:
var freq = 0.1; // angular frequency
var coords = function (x, y) {
var amplitude = Math.floor((Math.random() * 10) + 1);
var phase = Math.floor((Math.random() * 20) + 1);
return {
x: Math.sin(freq * (val1 + phase) * amplitude) + Math.floor((Math.random() * 100) + 1),
y: Math.sin(freq * (val2 + phase) * amplitude) + Math.floor((Math.random() * 100) + 1)
};
}
function genSine(val1, val2) {
setInterval(function () {
current = coords(50, 50);
console.log('Val 1: ' + current.x);
console.log('Val 2: ' + current.y);
}, 2000);
}
genSine(10, 20);
in typescript i did following code. in the below code i assign _this with the value of this
export class HomeComponent implements OnInit {
lat: number = 22.008515;
setMarker(){
console.log('Inside setMarker Lat : '+this.lat); // 22.008515
var _this = this;
setInterval(function() {
console.log('Inside setInterval this.lat : '+this.lat); // undefined
console.log('Inside setInterval _this.lat : '+_this.lat); // 22.008515
}, 8000)
}
}