Ad
JavaScript/HTML: How To Use Window.onload=x Together With Body OnLoad="setTimeout('myFunction()',4000);"
I'm using body onLoad="setTimeout('myFunction()',4000);"
to refresh my website every 4 seconds. I want to use another JavaScript that will make my text fade. It works, but then my website won't refresh every 4 seconds. It's either fade or refresh. They interfere with each other.
The text fading Script needs window.onload=fade
in order to work, but if I use it, it will overwrite the body onLoad="setTimeout('myFunction()',4000);"
- how do I make both work?
Ad
Answer
You can try:
body onLoad="fade(); setTimeout(myFunction, 4000);"
instead of the window.onload
call or use:
window.onload = function () {
fade();
setTimeout(myFunction, 4000);
};
instead of the body onload
.
You could also add multiple event listeners (as they are the preferred approaches) instead of using the above methods.
function onPageLoad() {
fade();
setTimeout(myFunction, 4000);
return;
};
if (document.addEventListener) {
document.addEventListener("load", onPageLoad);
}
else if (document.attachEvent) {
document.attachEvent("onload", onPageLoad);
}
else {
// event handling not supported
}
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad