I'm trying to execute some statements with setTimeout, inside the function plotReglaFalsa which is actually executed as callback at getSendingJSON("/plot",args,plotReglaFalsa)
This is is code snippet where sentences are executed by setTimeout:
for (series in respuesta) {
if (series != "x" && series != "y" && series != "raiz") {
setTimeout(function(respuesta,series){plot.highlight(c,[respuesta[series].a,0])},1500)
setTimeout(function(respuesta,series){plot.highlight(c,[respuesta[series].b,0])},1800)
c++
}
}
The issue here is that respuesta and so series are actually existing once the callback happens.
When I try to run I get the following console outputs:
TypeError: series is undefined ...Timeout(function(respuesta,series){plot.highlight(c,[respuesta[series].a,0])},15... 16 biseccion.js (line 50) TypeError: series is undefined ...Timeout(function(respuesta,series){plot.highlight(c,[respuesta[series].b,0])},18...
This is my whole code:
function plotReglaFalsa(respuesta) {
var result = []
result.push({
label: "fx",
color: "red",
data: _.zip(respuesta['x'], respuesta['y'])
})
for (series in respuesta) {
if (series != "x" && series != "y" && series != "raiz") {
result.push({
color: "blue",
data: [[]]
})
}
}
var plot = $.plot( $("#placeholder"),
result,
{ selection:{mode: "xy"},
zoom: { interactive: true },
pan: { interactive: true },
grid: { markings: [{ xaxis: { from: 0.0, to: 0.0 }, color: 'black', lineWidth: 2 }, { yaxis: { from: 0.0, to: 0.0 }, color: 'black', lineWidth: 2 }] }
})
plot.getOptions().selection.mode = null
var c = 1
for (series in respuesta) {
if (series != "x" && series != "y" && series != "raiz") {
setTimeout(function(respuesta,series){plot.highlight(c,[respuesta[series].a,0])},1500)
setTimeout(function(respuesta,series){plot.highlight(c,[respuesta[series].b,0])},1800)
c++
}
}
}
getSendingJSON("/plot",args,plotReglaFalsa)
function resaltarPuntos(plot,respuesta,series,c,x){
plot.highlight(c,[respuesta[series].x,0])
}
function desResaltarPuntos(plot){
plot.unhighlight()
}
getSendingJSON is actually AJAX. How can I get this completed?
setTimeout(), so thesetTimeout()is going to invoke them. Yet they have no idea what the variables are that you want, so they have no way to pass them to the functions. Defining function parameters doesn't somehow cause them to be passed to the function.setTimeout. Like:setTimeout(function(respuesta,series){/*...*/}, 1500, respuesta, series);Won't work in older browsers though.