JAVASCRIPT - "Code inside event" timeout? o.O -


i have event inside page

window.addeventlistener("load", function(){  	           var load_screen = document.getelementbyid("load_screen");  	           document.getelementsbytagname("body")[0].setattribute("class", "loaded");              })

wich adds attribute body class="loaded" when page loaded because of added cool preloader effect i'd make event wait 3-5 seconds before showing real page, if loaded (but of course wait more if not loaded). possible in way?

if want delay minimum of 5 seconds, can capture time @ point establish event handler (i.e., before "load" event has fired), , check time again in handler.

var starttime = date.now(); window.addeventlistener("load", function(){   var load_screen = document.getelementbyid("load_screen");   settimeout(function() {     document.getelementsbytagname("body")[0].setattribute("class", "loaded");   }, math.max(0, 5000 - (date.now() - starttime))); }); 

also there's no reason use .setattribute() set class:

    document.getelementsbytagname("body")[0].classname = "loaded"; 

personally i'd either use .classlist facility or go old-school , add new class:

    document.getelementsbytagname("body")[0].classname += " loaded"; 

that way don't step on other classes may (possibly in future) on <body>.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -