Hello people, I have a problem and I would like to know if anyone can help me.
I will give some context:
I have a main page, from which I navigate to another containing a circle, which enlarges and infinitely shrinks with css3 shapes. All this works well.
Within this circle I have a paragraph that changes dynamically from the controller. As the length of the word changes, I must dynamically change the ‘left’ property of the paragraph. Then I move it dynamically with javascript. This also works fine, but the problem occurs when I pop ().
When I pop (), return to the previous page and then about 1 second, I get this error:
TypeError: Can not read property 'style' of null
At BreathePage.centerInstruction (breathe.ts: 54)
Apparently, it’s as if the functions of the second page would continue to run even though I did pop().
It seems that it continues searching for this property being in the main page, reason why obviously does not exist.
This is my breathe.ts:
import { Component, NgZone } from '@angular/core';
import { NavController } from 'ionic-angular';
import * as $ from 'jquery';
@Component({
selector: 'page-breathe',
templateUrl: 'breathe.html'
})
export class BreathePage {
readonly BREATHE_INSTRUCTION_INHALE: string = 'Inhala';
readonly BREATHE_INSTRUCTION_HOLD: string = 'Mantiene';
readonly BREATHE_INSTRUCTION_EXHALE: string = 'Exhala';
// Must total 8000
readonly TIME_INHALING: number = 2500;//1800;
readonly TIME_HOLDING: number = 1300;//1700;
readonly TIME_EXHALING: number = 4100;//4300;
breatheInstruction: string;
constructor(public navCtrl: NavController,
private zone: NgZone
) {
this.breatheInstruction = this.BREATHE_INSTRUCTION_INHALE;
}
ionViewDidEnter() {
this.breatheInstruction = this.BREATHE_INSTRUCTION_INHALE;
this.switchInstruction();
}
public switchInstruction(): void {
this.breatheInstruction = this.BREATHE_INSTRUCTION_INHALE;
var hold = setTimeout(() => {
clearTimeout(hold);
this.breatheInstruction = this.BREATHE_INSTRUCTION_HOLD;
this.centerInstruction();
var exhale = setTimeout(() => {
clearTimeout(exhale);
this.breatheInstruction = this.BREATHE_INSTRUCTION_EXHALE;
this.reestablishInstructionPosition();
var repeat = setTimeout(() => {
clearTimeout(repeat);
this.switchInstruction();
}, this.TIME_EXHALING);
}, this.TIME_HOLDING);
}, this.TIME_INHALING);
}
public centerInstruction(): void {
/*$('.element p').css({
'left' : '36%'
});*/
document.getElementById('instruction').style.left = '36%';
}
public reestablishInstructionPosition(): void {
/*$('.element p').css({
'left' : '40%'
});*/
document.getElementById('instruction').style.left = '40%';
}
public breathClose(): void {
this.navCtrl.pop();
}
}
I tried doing this with jquery (as you can see the code commented) and does not throw errors, but it works poorly, the paragraph constantly changes its left property, does not respect the normal workflow.
Any ideas?
Thanks in advance!!