I'm new to programming and the following code I got through an open source project.
I'm loading a widget on a website. So instead of immediately loading the widget, I want it to take 10 secs before showing the widget.
This is how I'm loading the widget.
Entire code below............
switch (methodName) {
case 'init':
const loadedObject = Object.assign(defaultConfig, item[1]);
// the actual rendering of the widget
const wrappingElement =
loadedObject.element ?? win.document.body;
targetElement = wrappingElement.appendChild(
win.document.createElement('div')
);
targetElement.setAttribute('id', `widget-${instanceName}`);
render(targetElement, loadedObject);
// store indication that widget instance was initialized
win[`loaded-${instanceName}`] = true;
break;
default:
console.warn(`Unsupported method [${methodName}]`, item[1]);
}
}
CodePudding user response:
you should use the setTimeout function as such:
switch (methodName) {
case 'init':
const loadedObject = Object.assign(defaultConfig, item[1]);
setTimeout(function(){
// the actual rendering of the widget
const wrappingElement =
loadedObject.element ?? win.document.body;
targetElement = wrappingElement.appendChild(
win.document.createElement('div')
);
targetElement.setAttribute('id', `widget-${instanceName}`);
render(targetElement, loadedObject);
// store indication that widget instance was initialized
win[`loaded-${instanceName}`] = true;
}, 10000);
break;
default:
console.warn(`Unsupported method [${methodName}]`, item[1]);
}
}
as shown in this answer here