function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Venkat SainathVenkat Sainath 

Countdown timer on record save

Hello Guys,
I am working on Countdown timer, On clicking Save, I am fetching the time from Time__c field and on clicking save the timer will start counting down and once it becomes 0, a pop up will come. If I enter 10 in Time__c field then countdown should start from 10.
<apex:page standardController="Timer_Alert__c">
 <apex:form >
   <apex:pageblock >
      <apex:pageMessages id="showmsg"></apex:pageMessages>
         <apex:panelGrid columns="2">
           Timer_Alert__c Time: <apex:inputText value="{!Timer_Alert__c.Time__c}"/>
           <apex:commandButton value="Save" action="{!save}" oncomplete="timer()" style="width:90px" rerender="showmsg"/>
         </apex:panelGrid>
                     <div align="center" draggable="false" >
         <script>
    function startTimer(duration, display) {
        var start = Date.now(),
            diff,
            minutes,
            seconds;
        function timer() {
            diff = duration - (((Date.now() - start) / 1000) | 0);
            
            minutes = (diff / 60) | 0;
            seconds = (diff % 60) | 0;
            
            minutes = minutes < 10 ? "0" + minutes : minutes;
            seconds = seconds < 10 ? "0" + seconds : seconds;
            
            display.textContent = minutes + ":" + seconds; 
            
            if (diff <= 0) {
                start = Date.now() + 1000;
            }
            if(diff==0){
                alert('STOP!!!');
                clearInterval(interval);
            }
        };
        timer();
        var interval = setInterval(timer, 1000);
    }
    
    window.onload = function () {
        var fiveMinutes = 60 * {!Timer_Alert__c.Time__c},
            display = document.querySelector('#time');
        startTimer(fiveMinutes, display);
    };
    </script>
    <style>
        h1 {
        font-size: 40px;
        text-align: center;
        background-color: orange;
        }
    </style>
    
    <style>
        h1 {
            font-size: 40px;
            text-align: center;
            background-color: orange;
        }
    </style>

            </div>
    </apex:pageblock>
 </apex:form>
</apex:page>


I have created the following VF Page but its not working, can anyone help me with the same.
Ashish Arun WaghmareAshish Arun Waghmare
Hi Venkat,

There is a function in Javascript that you can make use of

setTimeout(function, milliseconds, param1, param2, ...);

So this can be used as 

setTimeout(NotifyFunction, 1000 * the field you are storing in the object {!Timer_Alert__c.Time__c} );

And you can show a notification once that is reached in the function.

notifyFunction {

var notification = new Notification('Chat Time Exceeded', { body: "Your message here"});

}


Hope this Helps !!!