+ Start a Discussion
Pankaj ShakyaPankaj Shakya 

How to use a showToast as a notification alert that will pop up when the timer hits a specific time?

Hi everyone

Below is my code for showToast but it invokes on doinit(). The problem is I have to reload the page to see the Toast as a notification. Is there a way to fire the toastEvent automatically on a specific time. 

doInit : function(component, event, helper) {
        var action = component.get("c.getAlertDateTime");
            action.setCallback(this, function(response){
                var state = response.getState();
                if(state === "SUCCESS"){
                    var result = response.getReturnValue();
                    var date = new Date();
                    var Today = date.toISOString();
                    for(var i=0; i!=result.length; i++){
                        if(result[i].pshak__Alert_Date_Time__c <= Today){
                            var toastEvent = $A.get("e.force:showToast");
                            toastEvent.setParams({
                                title : 'Note Reminder',
                                message: 'Check out your note \''+result[i].Name+'\'',
                                type: 'warning',
                                mode: 'sticky',
                                });
                            toastEvent.fire();
                        }
                    }
                    component.set("v.notesList", result);
                }
            });
            $A.enqueueAction(action);
    }

Thanks.
 
Best Answer chosen by Pankaj Shakya
Divya Agrawal 14Divya Agrawal 14
Hi Pankaj,

The following code will show toast in every minute
Controller:
({
    init: function (component, event, helper) {
        helper.everyMinute(component,helper);
    }
})

Helper:

({
	
	everyMinute : function(component,helper){ 
 		alert('Hello');
        
        //Show Toast code Here
        
        window.setTimeout( $A.getCallback(function() 
                                          { 
                                              console.log('Calling'); 
                                              helper.everyMinute(component,helper);
                                          }), 60000 ); 
	}
})


Following code for showing toast only once after the page has been load after 10 seconds
Controller:
({
    init: function (component, event, helper) {
      
     window.setTimeout( $A.getCallback(function() 
                                          { 
                                              console.log('Calling'); 
                                              helper.everyMinute(component,helper);
                                          }), 10000 ); 
	}
    }
})

Helper:

({
	
	everyMinute : function(component,helper){ 
 		alert('Hello');
        //Show Toast code Here
        
      
})

Dear Reader, Please mark as the best answer

Thanks & Regards,
Divya