I have a page that automatically loads changes every 2 seconds and inside it has a search engine that, when writing for a setInterval
, keeps searching.
In principle everything works with this code:
//refrescador del buscador
setTimeout(function(){
$(".resultados_Inarticulos").load("Paginas/Articulos.php");
}, 10); //lo ejecuto nada mas entrar
var Pausado_Inarticulo = false;
var tiempo_Inarticulos = window.setInterval(function() {
if(!Pausado_Inarticulo) {
$(".resultados_Inarticulos").load("Paginas/Articulos.php");
}
}, 2000);
$("#buscador_Inarticulos").keyup(function(){
if ($('#buscador_Inarticulos').val() != null && $('#buscador_Inarticulos').val() != '') {
Pausado_Inarticulo = true;
} else {
Pausado_Inarticulo = false;
}
}); //si esta vacio el buscador paro el refresco, sino sigo refrescando
//fin de refrescador del buscador
The thing is that I want to globalize that function so I don't have to write it in each of the pages and pass it to a page of common functions for all of them. For this I only have to be able to change Pausado_Inarticulo
to Pausado_XXX
and I would like to put something like:
Pausar('Inarticulos');
And that this call a generic function that simply passing it the data of Inarticles executes it in the Inarticles page.
What syntax do I have to use? I have tried to put this in the functions page, but nothing:
function Pausar(pagina,div,url) {
//refrescador del buscador
setTimeout(function(){
$("."+div).load("Paginas/"+url);
}, 10); //lo ejecuto nada mas entrar
var Pausado_+pagina = false;
var tiempo_+pagina = window.setInterval(function() {
if(!Pausado_+pagina) {
$("."+div).load("Paginas/"+url);
}
}, 2000);
$("#buscador_"+pagina).keyup(function(){
if ($('#buscador_'+pagina).val() != null && $('#buscador_'+pagina).val() != '') {
Pausado_+pagina = true;
} else {
Pausado_+pagina = false;
}
}); //si esta vacio el buscador paro el refresco, sino sigo refrescando
//fin de refrescador del buscador
};
But it doesn't go and it gives me a syntax error in the variable var Pausado_+pagina = false;
, so I deduce that this way of constructing that variable is wrong.
I need that variable not to have a common name since the page loads the different subpages at the same time within the page itself, so I cannot call the variables with the same name or it will affect all of them at the same time. I have to be able to differentiate it in some way.
You get an error because
+
it's not a character that can be used in a variable name, nor dynamically to create a name on the fly (as you intend).What you could do is use that variable (which I assume contains a unique page identifier) as an index in an associative array (simplifying: the indices will be strings instead of numbers). In that case, you could generalize the creation of variables for each page. For example, for the "abc" page, you would do
Pausado["abc"]
andtiempos["abc"]
; for "xyz" it would bePausado["xyz"]
, etc.So you could do something like this: