Time Availability
Runnable Cesium example: Time Availability. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:用时间区间控制实体出现与消失
要点:TimeIntervalCollection + TimeInterval 描述窗口,按 clock 时间逐帧设置 show
依赖:CesiumJS 1.121 · jsDelivr CDN,窗口时间写在页面里
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>时间区间显隐</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Widgets/widgets.css">
<style>
html,body{height:100%;margin:0}
body{background:#0e1116;color:#e6e8ec;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
#c{height:100%}
.hud{position:absolute;left:12px;top:12px;z-index:5;background:rgba(23,28,35,.92);
border:1px solid #2a313b;border-radius:6px;padding:9px 12px;font-size:12px;
color:#a2a9b6;width:262px}
.hud b{color:#e6e8ec;font-weight:600}
.hud .row{margin-top:6px}
.hud button{font:inherit;font-size:12px;padding:3px 9px;border-radius:4px;cursor:pointer;
border:1px solid #343b46;background:#1d232b;color:#cfd6e2}
.win{display:flex;justify-content:space-between;line-height:1.9;color:#7f8896}
.win.on{color:#cfe4ff}
.mono{font-family:ui-monospace,Consolas,monospace;font-size:11px;color:#8fb7e0}
.cesium-viewer-bottom .cesium-widget-credits,.cesium-viewer-animationContainer,
.cesium-viewer-timelineContainer{display:none!important}
</style>
</head>
<body>
<div id="c"></div>
<div class="hud">
<div><b>时间区间显隐</b></div>
<div class="row mono" id="now">—</div>
<div class="row" id="w0"><span class="win">A 00:00 ~ 00:40</span></div>
<div class="row" id="w1"><span class="win">B 00:30 ~ 01:10</span></div>
<div class="row" id="w2"><span class="win">C 01:00 ~ 01:30</span></div>
<div class="row"><button id="toggle">暂停</button></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/cesium@1.121.0/Build/Cesium/Cesium.js"></script>
<script>
(function () {
'use strict';
var viewer = new Cesium.Viewer('c', {
baseLayer: false,
baseLayerPicker: false, geocoder: false, homeButton: false,
sceneModePicker: false, navigationHelpButton: false,
animation: false, timeline: false, fullscreenButton: false,
infoBox: false, selectionIndicator: false
});
var scene = viewer.scene;
scene.globe.baseColor = Cesium.Color.fromCssColorString('#1c2733');
scene.backgroundColor = Cesium.Color.fromCssColorString('#0e1116');
scene.skyBox.show = false;
scene.sun.show = false;
scene.moon.show = false;
if (scene.skyAtmosphere) scene.skyAtmosphere.show = false;
var START = Cesium.JulianDate.fromIso8601('2026-10-28T00:00:00Z');
var clock = viewer.clock;
clock.startTime = START.clone();
clock.stopTime = Cesium.JulianDate.addSeconds(START, 5400, new Cesium.JulianDate());
clock.currentTime = START.clone();
clock.multiplier = 60;
clock.clockRange = Cesium.ClockRange.LOOP_STOP;
clock.shouldAnimate = true;
function at(minute) {
return Cesium.JulianDate.addSeconds(START, minute * 60, new Cesium.JulianDate());
}
/* 三个站点的可用窗口,最小单位是分钟 */
var STATIONS = [
{ name: 'A', lng: 116.374, lat: 39.906, from: 0, to: 40, color: '#4d9bff' },
{ name: 'B', lng: 116.389, lat: 39.906, from: 30, to: 70, color: '#5fd0a8' },
{ name: 'C', lng: 116.404, lat: 39.906, from: 60, to: 90, color: '#ffb13d' }
];
STATIONS.forEach(function (s) {
var entity = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(s.lng, s.lat, 0),
point: {
pixelSize: 14,
color: Cesium.Color.fromCssColorString(s.color),
outlineColor: Cesium.Color.WHITE,
outlineWidth: 2,
disableDepthTestDistance: Number.POSITIVE_INFINITY
},
label: {
text: '站点 ' + s.name,
font: '600 13px "Microsoft YaHei", sans-serif',
fillColor: Cesium.Color.fromCssColorString(s.color),
pixelOffset: new Cesium.Cartesian2(0, -22),
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
});
s.entity = entity;
s.intervals = new Cesium.TimeIntervalCollection([
new Cesium.TimeInterval({
start: at(s.from),
stop: at(s.to),
isStartIncluded: true,
isStopIncluded: true
})
]);
/* 同时把可用窗口挂到实体上,数据导出或换到数据源时同样生效 */
entity.availability = s.intervals;
});
/* 连接三个站点的线,也跟着时间显隐 */
var link = viewer.entities.add({
polyline: {
positions: Cesium.Cartesian3.fromDegreesArray([
116.374, 39.906, 116.389, 39.906, 116.404, 39.906
]),
width: 2,
material: new Cesium.PolylineDashMaterialProperty({
color: Cesium.Color.fromCssColorString('#3f5a78'),
dashLength: 20
}),
clampToGround: true
}
});
/* 每帧按当前 clock 时间决定显隐,逻辑一目了然 */
var nowEl = document.getElementById('now');
var rowEls = [
document.getElementById('w0'),
document.getElementById('w1'),
document.getElementById('w2')
];
scene.preRender.addEventListener(function () {
var active = 0;
STATIONS.forEach(function (s, i) {
var visible = s.intervals.contains(clock.currentTime);
s.entity.show = visible;
rowEls[i].firstElementChild.className = visible ? 'win on' : 'win';
if (visible) active++;
});
link.show = active >= 2;
nowEl.textContent = Cesium.JulianDate.toIso8601(clock.currentTime, 0).slice(11, 19);
});
var paused = false;
document.getElementById('toggle').addEventListener('click', function () {
paused = !paused;
clock.shouldAnimate = !paused;
this.textContent = paused ? '继续' : '暂停';
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.389, 39.888, 3200),
orientation: {
heading: 0,
pitch: Cesium.Math.toRadians(-32),
roll: 0
},
duration: 0
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.