Destroy a javascript action

Destroy a javascript action

  

Hi,

I am trying to add a time count logic using javascript in a web block,

What I did is, in On ready action I implemented this code

var deadline = new Date($parameters.Datetime).getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var t = deadline - now;
var days=Math.floor(t / (1000 * 60 * 60 * 24));
var hours=Math.floor((t%(1000 * 60 * 60 * 24))/(1000 * 60 * 60));
var minutes=Math.floor((t % (1000 * 60 * 60)) / (1000 * 60));
var seconds=Math.floor((t % (1000 * 60)) / 1000);
document.getElementById($parameters.Day).innerHTML =days ;
document.getElementById($parameters.Hour).innerHTML =hours;
document.getElementById($parameters.Minute).innerHTML = minutes;
document.getElementById($parameters.Seconds).innerHTML =seconds;
if (t < 0) {
        clearInterval(x);
        document.getElementById($parameters.Paragraph).innerHTML = "TIME UP";
        document.getElementById($parameters.Day).innerHTML ='0';
        document.getElementById($parameters.Hour).innerHTML ='0';
        document.getElementById($parameters.Minute).innerHTML ='0' ;
        document.getElementById($parameters.Seconds).innerHTML = '0'; }
}, 1000);



now I need to destroy the action in OnDestroy since I am not that much expert in javascript.

can anyone suggest me how to do that?


The problem I am facing is initially I will pass the date and time value to the block.

when I change the value and there were two counts running simultaneously.

I want to stop the timer count of the first passed value


Thanks

Keerthi.

HI Keerthi,

setInterval returns a value that you can use in clearInterval to stop the timer. Also, you need to clear in the OnDestroy of the Screen.

Kilian Hekhuis wrote:

HI Keerthi,

setInterval returns a value that you can use in clearInterval to stop the timer. Also, you need to clear in the OnDestroy of the Screen.

Still the same issue, The timer is running .. i dont know whether i made some mistakes or not


on ready script

var deadline = new Date($parameters.Datetime).getTime();
clearInterval($parameters.Con);
$parameters.Con=setInterval(function() {
var now = new Date().getTime();
var t = deadline - now;
var days=Math.floor(t / (1000 * 60 * 60 * 24));
var hours=Math.floor((t%(1000 * 60 * 60 * 24))/(1000 * 60 * 60));
var minutes=Math.floor((t % (1000 * 60 * 60)) / (1000 * 60));
var seconds=Math.floor((t % (1000 * 60)) / 1000);
document.getElementById($parameters.Day).innerHTML =days ;
document.getElementById($parameters.Hour).innerHTML =hours;
document.getElementById($parameters.Minute).innerHTML = minutes;
document.getElementById($parameters.Seconds).innerHTML =seconds;
if (t < 0) {
        clearInterval($parameters.Con);
        document.getElementById($parameters.Paragraph).innerHTML = "TIME UP";
        document.getElementById($parameters.Day).innerHTML ='0';
        document.getElementById($parameters.Hour).innerHTML ='0';
        document.getElementById($parameters.Minute).innerHTML ='0' ;
        document.getElementById($parameters.Seconds).innerHTML = '0'; }
}, 1000);

On Destroy script

clearInterval($parameters.Con);
document.getElementById($parameters.Day).innerHTML =0 ;
document.getElementById($parameters.Hour).innerHTML =0;
document.getElementById($parameters.Minute).innerHTML = 0;
document.getElementById($parameters.Seconds).innerHTML =0;



$parameters.Con is the local variable

That should do the trick, I've used it like that myself. Have you checked the browser's console to see whether any errors are displayed?

Kilian Hekhuis wrote:

That should do the trick, I've used it like that myself. Have you checked the browser's console to see whether any errors are displayed?

https://keerthi-vasan.outsystemscloud.com/PreviewInDevices/?IsMobilePreview=True&DeviceName=Smartphone&URL=/CountDown/CountDown?_ts=636675292429757106

Can you please check the link,

Go to the link, select the date and time, click start

The countdown will start loading,

now go to the menu and click home screen again

now enter another date and time and click start,

you can see the old countdown and new countdown running simultaneously.


that's the issue I am facing


Thanks,

Keerthi

Can you share the OML (eSpace) for that?

Please Check the attachment

Hi Keerthi,

I don't see an OnDestroy defined for the Home Screen. It should be defined here:

Inside the Screen Action, put a JavaScript Statement with the clearInterval().

I did as you said,

I got an error clear interval is not defined.


can you do that modification on that oml that I sent earlier? and can you share that?

Well, if it complains about the interval not being defined, you don't store and use the right result from the setInterval. I can't modify and test the eSpace, as I can't publish it in my Personal Environment.

Thanks, Kilian.

I will do some R&D on that and will try to resolve it myself

I hope you can make it work Keerthi!