我已按照教程:http://www.javascriptkit.com/dhtmltutors/live-local-time-google-time-zone-api.shtml
现在我想把它分成两个函数:
功能#1。以毫秒为单位返回设置时区的当前时间
功能#2。将函数#1中的localdate对象每秒递增1秒,然后将其插入
但我得到输出"无效日期" 这是我的代码:
var loc = '37.7749295, -122.4194155'; // San Francisco expressed as lat, lng tuple
var targetDate = new Date(); // Current date/time of user computer
var timestamp = targetDate.getTime() /1000 + targetDate.getTimezoneOffset() * 60; // Current UTC date/time expressed as seconds since midnight, January 1, 1970 UTC
var apikey = 'Google_Time_Zone_key'; // timezone API key
var apicall = 'https://maps.googleapis.com/maps/api/timezone/json?location=' + loc +'×tamp=' + timestamp + '&key=' + apikey;
function current_time_load1 () {
var xhr = new XMLHttpRequest (); // create new XMLHttpRequest2 object
xhr.open ('GET', apicall); // open GET request
xhr.onload = function() {
if (xhr.status === 200) { // if Ajax request successful
var output = JSON.parse(xhr.responseText); // convert returned JSON string to JSON object
// console.log(output.status); // log API return status for debugging purpose
if (output.status =='OK') { // if API reports everthing was returned sucessfully
var offsets = output.dstOffset * 1000 + output.rawOffset * 1000; // get DST and time zone offsets in milliseconds
var localdate = new Date (timestamp * 1000 + offsets); // Date object containing current time of destination (timestamp + dstOffset + rawOffset)
// localdate.toLocaleString(); // Display current destination date and time
var refreshDate = new Date(); // get current date again to calculate time elapsed between targetDate and now
var millisecondselapsed = refreshDate - targetDate; // get amount of time elapsed between targetDate and now
localdate.setMilliseconds(localdate.getMilliseconds()+ millisecondselapsed); // update localdate to account for any time elapsed
}
}
else {
console.log ('Request failed. Returned status of ' + xhr.status);
} //end of xhr.onload
}
xhr.send(); // send request
}
function current_time_load(divid){
var container = document.getElementById(divid);
var localdate = new Date (current_time_load1 ());
setInterval(function(){
localdate.setSeconds(localdate.getSeconds()+1);
container.innerHTML = localdate.toLocaleTimeString('zh', {year : 'numeric', month: 'short', day: 'numeric'});
}, 1000);
}
current_time_load('time');

<div>
test current time: <span id="time"></span>
</div>
&#13;
我确实在console.log中检查了current_time_load1()的值,显示未定义,但是console.log(localdate.setMilliseconds(localdate.getMilliseconds()+ millisecondselapsed);)确实返回了毫秒
你能帮忙指出分裂的错误吗?
答案 0 :(得分:0)
几个小时的搜索,我终于意识到它需要一个回调来检索XmlHttpRequest的结果。这对我有用:
function current_time_load1 (callback) {
var xhr = new XMLHttpRequest (); // create new XMLHttpRequest2 object
xhr.open ('GET', apicall); // open GET request
xhr.onload = function() {
if (xhr.status === 200) { // if Ajax request successful
var output = JSON.parse(xhr.responseText); // convert returned JSON string to JSON object
// console.log(output.status); // log API return status for debugging purpose
if (output.status =='OK') { // if API reports everthing was returned sucessfully
var offsets = output.dstOffset * 1000 + output.rawOffset * 1000; // get DST and time zone offsets in milliseconds
var localdate = new Date (timestamp * 1000 + offsets); // Date object containing current time of destination (timestamp + dstOffset + rawOffset)
// localdate.toLocaleString(); // Display current destination date and time
var refreshDate = new Date(); // get current date again to calculate time elapsed between targetDate and now
var millisecondselapsed = refreshDate - targetDate; // get amount of time elapsed between targetDate and now
if(callback) {
callback(localdate.setMilliseconds(localdate.getMilliseconds()+ millisecondselapsed));
} // update localdate to account for any time elapsed
//console.log(localdate.setMilliseconds(localdate.getMilliseconds()+ millisecondselapsed));
}
}
else {
console.log ('Request failed. Returned status of ' + xhr.status);
} //end of xhr.onload
}
xhr.send(); // send request
}
current_time_load1(function (result){
console.log('okay');
var container = document.getElementById('test');
var localdate = new Date(result);
setInterval(function(){
localdate.setSeconds(localdate.getSeconds()+1);
container.innerHTML = localdate.toLocaleTimeString('zh', {year : 'numeric', month: 'short', day: 'numeric'});
}, 1000);
}, function(){
console.log('fail');
});
所以,我基本上将if(callback){callback();}
添加到current_time_load1函数中。然后使用包含“result”参数的新函数运行此函数。一个很好的教训:)