简体   繁体   中英

Javascript How do I add a delay on displaying the DOM element?

I need to add a delay in displaying a page in my app. I am using angularjs framework. The reason why i need to do this is because, even though my DOM is fully rendered, the data from angularjs is still being read and not added to the DOM. I have tried adding timeout to the ui-view BUT it doesnt do the trick.

Try to use timeout function :

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
    $scope.myHeader = "Hello World!";
    $timeout(function () {
        $scope.myHeader = "How are you today?";
    }, 2000);
});

More Information ...

I didn't get why you want to delay page loading

What I understood from your problem is that your page is getting displayed in uncompiled form
try to use

ng-cloack directive

The ngCloak directive is used to prevent the Angular html template from being briefly displayed by the browser in its raw (uncompiled) form while your application is loading. Use this directive to avoid the undesirable flicker effect caused by the html template display.

The directive can be applied to the <body> element, but the preferred usage is to apply multiple ngCloak directives to small portions of the page to permit progressive rendering of the browser view.

here is complete documentation

https://docs.angularjs.org/api/ng/directive/ngCloak

Use the resolve option to load your JSON before the view is loaded. Documentation.

For example:

resolve: {
    return $http({method: 'GET', url: '/someUrl'})
               .then (function (data) {
                   return doSomeStuffFirst(data);
               });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM