Search code examples
javascriptangularjstimeoutsettimeout

$timeout runs instantly instead of waiting for the timeout


I'm working on a service that shows a notification and hides it after some specific time, but the code in $timeout runs instantly, I know that the showing part works because when I run it line by line I can see it, but as soon as I go to the timeout line it is hidden, the time out in this section is set for 10 seconds. below is the $timeout part

    $timeout(() => {
        var domElement = angular.element(
            document.querySelector('#toast-notification')
        );
        domElement.removeClass('show');
    }, data.eventDuration);

Solution

  • Note that data.eventDuration has to be 10000 for 10 seconds, because $timeout is working with milliseconds.

    var data = {};
    data.eventDuration = 10000;
    // now your function should be called after 10 seconds.
    
    $timeout(() => {
        var domElement = angular.element(
            document.querySelector('#toast-notification')
        );
        domElement.removeClass('show');
    }, data.eventDuration);