作者:文竹 | 来源:互联网 | 2023-10-13 17:21
用JavaScript实现的太空人手表-JS写的太空人手表,没有用canvas、svg。主要用几个大的函数来动态显示时间、天气这些。天气的获取用到了AJAX请求。代码中有详细的注释
JS写的太空人手表,没有用canvas、svg。
主要用几个大的函数来动态显示时间、天气这些。
天气的获取用到了AJAX请求。
代码中有详细的注释,可以通过看注释了解。
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021033118492454.gif#pic_center
主要是JS部分
setInterval(function time () {
var data=new Date()
var hour=data.getHours();
var min=data.getMinutes()
var second=data.getSeconds()
if(second<10){
second="0"+second;
}
if(min<10){
min="0"+min;
}
if(hour<10){
hour="0"+hour
}
var p=document.getElementById("p")
p.innerText=hour+":"+min+":"+second
},10)
var xmlhttp;
function wether(){
$.ajax({
url:"http://wthrcdn.etouch.cn/weather_mini?city=成都",
dataType:"json",
async:false,
success:function(data){
function tianqi(){
xmlhttp=data;
return xmlhttp;
};
tianqi();
}
})
console.log(xmlhttp);
}
setInterval(wether(),1000);
var high=xmlhttp.data.forecast[0].high
var low=xmlhttp.data.forecast[0].low;
high=high.replace(/[高温 ]/g,"")
low=low.replace(/[低温 ]/g,"")
var now=xmlhttp.data.wendu;
var img=["./太阳.png","./多云.png","./霾.png","./下雨.png"]
var weather=xmlhttp.data.forecast[0].type;
function tem(){
let temhigh=document.getElementById("tem-high");
let temlow=document.getElementById("tem-low");
temlow.innerText=low
temhigh.innerText=high
let wh=document.getElementById("weather");
wh.innerText=weather
var whnow=document.getElementById("now");
whnow.innerText=now+"℃"
}
setInterval(tem(),1000);
function whimg(){
let sun=document.getElementById("sun");
if(weather=="多云"||weather=="阴"){
sun.src=img[1];
}
else if(weather=="霾"){
sun.src=img[2];
}
else if(/[雨]/g.test(weather)){
sun.src=img[3]
}
else{
sun.src=img[0]
}
}
setInterval(whimg(),1000)
function shijian(){
let date=new Date();
let month=date.getMonth()+1;
let day=date.getDate();
let week=date.getDay();
let timer=document.getElementById("time");
let number=["一","二","三","四","五","六","日"];
if(week==1){
week=number[0];
timer.innerHTML=month+"月"+day+"日"+"
"+"星期"+week;
}else if(week==2){
week=number[1];
timer.innerHTML=month+"月"+day+"日"+"
"+"星期"+week;
}else if(week==3){
week=number[2];
timer.innerHTML=month+"月"+day+"日"+"
"+"星期"+week;
}else if(week==4){
week=number[3];
timer.innerHTML=month+"月"+day+"日"+"
"+"星期"+week;
}else if(week=5){
week=number[4]
timer.innerHTML=month+"月"+day+"日"+"
"+"星期"+week;
}else if(week==6){
week=number[5];
timer.innerHTML=month+"月"+day+"日"+"
"+"星期"+week;
}else {
timer.innerHTML=month+"月"+day+"日"+"
"+"星期"+week;
}
}
shijian();