How to implement displaying different ads at different times of the day in JavaScript?
You can dynamically determine which ads to display by getting the current time. Here is a simple example code:
// 获取当前时间
var currentTime = new Date();
var currentHour = currentTime.getHours();
// 根据时间条件展示不同的广告
if (currentHour < 12) {
// 上午展示广告1
document.getElementById("ad").innerHTML = "<img src='ad1.jpg'>";
} else if (currentHour < 18) {
// 下午展示广告2
document.getElementById("ad").innerHTML = "<img src='ad2.jpg'>";
} else {
// 晚上展示广告3
document.getElementById("ad").innerHTML = "<img src='ad3.jpg'>";
}
Add an element to the page with the ID “ad” to display different advertisement images. Use the code above to show different ads based on the current time. Visitors to the page will see the corresponding ad image based on the current time.