JavaScript - Problema con funcion setInterval

 
Vista:

Problema con funcion setInterval

Publicado por Carlos (2 intervenciones) el 09/10/2016 23:06:56
Buenos dias/tardes/noches... espero que todos se encuentren bien, me dirijo hoy a uds. a ver si por favor me pueden solucionar un problema con la funcion setInterval de JavaScript estoy haciendo un contador regresivo para una serie de eventos que ocurren en X dia de la semana, toda la funcionalidad del archivo js esta operativa menos la del setInterval, no encuentro en que lugar esta la falla y quisiera saber si alguno de uds. podria ayudarme... de antemano mil gracias.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var horaEv, horas, minutos, segundos, tServidor, tEvento, timer;
 
var fecha = new Date();
 
	function obtenerDia(){
		var dia = fecha.getUTCDay();
			return dia;
	}
 
	function obtenerHora(){
		horas = fecha.getUTCHours();
		minutos = fecha.getUTCMinutes();
		segundos = fecha.getUTCSeconds();
		horas -= 4;
	}
 
	function evento(horaEvento){
		horaEv = horaEvento.split(":");
		for(var i = 0; i < horaEv.length; i++){
			horaEv[i] = parseInt(horaEv[i]);
		}
	}
 
	function convertirASegundos(){
		var tEspera;
 
		tServidor = ((horas*3600) + (minutos*60)) + segundos;
		tEvento = (horaEv[0]*3600) + (horaEv[1]*60) + horaEv[2];
		tEspera = tEvento - tServidor;
		return tEspera;
	}
 
	function evaluarTiempos(){
		var eval;
 
		if(tServidor > tEvento){
			eval = false;
		}
		else{
			eval = true;
		}
 
		return eval;
	}
 
	function tw(dia){
		var bool, tEspera;
 
		if(dia == 0){
 
			evento('13:30:00');
			tEspera = convertirASegundos();
			bool = evaluarTiempos();
 
			if(bool == false){
 
				evento('17:00:00');
				tEspera = convertirASegundos();
				bool = evaluarTiempos();
 
				if(bool == false){
 
					evento('21:00:00');
					tEspera = convertirASegundos();
					bool = evaluarTiempos();
				}
			}
 
			convertirAHoras(tEspera, "hora");
		}
	}
 
	function cronograma(){
		var dia = obtenerDia();
 
		obtenerHora();
		tw(dia);
		timer = setInterval(tw, 1000);
	}
 
	function convertirAHoras(tiempo, contenedor){
		var h, m, s, aux;
 
		h = Math.trunc(tiempo/3600);
		m = Math.trunc((tiempo - (h*3600))/60);
		s = Math.trunc(tiempo - ((h*3600) + (m*60)));
		document.getElementById(contenedor).textContent = h+":"+m+":"+s;
	}
 
	document.addEventListener("DOMContentLoaded", cronograma, false);
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.162
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Problema con funcion setInterval

Publicado por xve (2100 intervenciones) el 10/10/2016 08:42:24
Hola Carlos, el setInterval() te funcionar perfectamente... el problema que tienes, es que desde setInterval() no pasas el parámetro dia que espera la función tw()... por eso no hace nada la función tw().

Para pasar el parámetro, puedes hacerlo así:
setInterval(function,milliseconds,param1,param2,...)
http://www.w3schools.com/jsref/met_win_setinterval.asp

1
timer = setInterval(tw, 1000, dia);
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Problema con funcion setInterval

Publicado por Carlos (2 intervenciones) el 11/10/2016 04:08:57
Buenos dias/tardes/noches xve, gracias por tu respuesta, hice las correciones que me dijiste pero igual sigue sin funcionar el reloj queda solo parado en la primera hora que obtiene no se aplica la funcion set interval :C aca dejo el codigo como lo tnego ahora. No termino de ver el por que falla todavia el setInterval, no se si esta teniendo compatibilidad con algun proceso quizas

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var horaEv, horas, minutos, segundos, tServidor, tEvento;
 
var fecha = new Date();
 
	function obtenerDia(){
		var dia = fecha.getUTCDay();
			return dia;
	}
 
	function obtenerHora(){
		horas = fecha.getUTCHours();
		minutos = fecha.getUTCMinutes();
		segundos = fecha.getUTCSeconds();
		horas -= 4;
	}
 
	function evento(horaEvento){
		horaEv = horaEvento.split(":");
		for(var i = 0; i < horaEv.length; i++){
			horaEv[i] = parseInt(horaEv[i]);
		}
	}
 
	function convertirASegundos(){
		var tEspera;
 
		tServidor = ((horas*3600) + (minutos*60)) + segundos;
		tEvento = (horaEv[0]*3600) + (horaEv[1]*60) + horaEv[2];
		tEspera = tEvento - tServidor;
		return tEspera;
	}
 
	function evaluarTiempos(){
		var eval;
 
		if(tServidor > tEvento){
			eval = false;
		}
		else{
			eval = true;
		}
 
		return eval;
	}
 
	function tw(dia){
		var bool, tEspera;
 
		if(dia > 0){
 
			evento('13:30:00');
			tEspera = convertirASegundos();
			bool = evaluarTiempos();
 
			if(bool == false){
 
				evento('17:00:00');
				tEspera = convertirASegundos();
				bool = evaluarTiempos();
 
				if(bool == false){
 
					evento('21:00:00');
					tEspera = convertirASegundos();
					bool = evaluarTiempos();
				}
			}
 
			convertirAHoras(tEspera, "hora");
		}
	}
 
	function cronograma(){
		var dia = obtenerDia();
 
		obtenerHora();
		tw(dia);
	}
 
	function convertirAHoras(tiempo, contenedor){
		var h, m, s, aux;
 
		h = Math.trunc(tiempo/3600);
		m = Math.trunc((tiempo - (h*3600))/60);
		s = Math.trunc(tiempo - ((h*3600) + (m*60)));
		document.getElementById(contenedor).textContent = h+":"+m+":"+s;
	}
 
	var timer = setInterval(cronograma, 1000);
	document.addEventListener("DOMContentLoaded", cronograma, false);
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar