我的任务是让小测验应用程序正常工作。我将问题保存为private static void SetTimer(SocketUser user)
{
var account = UserAccounts.GetAccount(user);
account.TimerOn = true;
timer = new System.Timers.Timer(1000); //(1000 * 60) * 10
timer.Elapsed += OnTimedEvent;
timer.Enabled = true;
timer.AutoReset = false;
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Console.WriteLine("Timer ended {0}", e.SignalTime);
}
文件并执行json
请求。当我初始化运行http
个问题时,它会呈现给浏览器。
JSON.parse()
但是,当我重构它以进行var output = document.getElementById('output');
// this allows us to keep it as a global value
var myObj = '';
loadQuestions();
console.log(myObj);
// var myQuestions = '[{"question":"What is your nearest star?","answers":{"a":"Alpha Centauri","b":"Barnard\'s Star","c":"Sirius","d":"Sol"},"correctAnswer":"d"},{"question":"What color is grass?","answers":{"a":"Blue","b":"Red","c":"Green","d":"Purple"},"correctAnswer":"c"}]';
// var myObj = JSON.parse(myQuestions);
// for (var i in myObj) {
// output.innerHTML += myObj[i].question + '? <br>';
// }
调用时,它会在控制台中迭代,但不会在浏览器窗口中迭代:
AJAX
我认为它可能是我的var output = document.getElementById('output');
// this allows us to keep it as a global value
var myObj = '';
loadQuestions();
console.log(myObj);
function loadQuestions() {
// http request
// this will allow you to pull in
// the questions from the api that
// your questions or data is stored in
var a = new XMLHttpRequest();
a.open("GET", "https://api.myjson.com/bins/8xmud", true); // opened request with address with
a.onreadystatechange = function(){
if (a.readyState == 1) {
var myQuestions = JSON.parse(a.responseText);
console.log(myQuestions);
for (var i in myQuestions) {
output.innerHTML = myQuestions[i].question + '? <br>';
}
}
console.log(a);
}
a.send();
}
循环,但这是正确的。我不清楚为什么,尽管得到for...in
或Status: OK
,它并没有像以前一样在浏览器窗口中迭代。
答案 0 :(得分:1)
var output = document.getElementById('output');
// this allows us to keep it as a global value
var myObj = '';
loadQuestions();
console.log(myObj);
function loadQuestions() {
// http request
// this will allow you to pull in
// the questions from the api that
// your questions or data is stored in
var a = new XMLHttpRequest();
a.open("GET", "https://api.myjson.com/bins/8xmud", true); // opened request with address with
a.onreadystatechange = function(){
if (a.readyState == 4) {
var myQuestions = JSON.parse(a.responseText);
console.log(myQuestions);
for (var i in myQuestions) {
output.innerHTML += myQuestions[i].question + '? <br>';
}
}
console.log(a);
}
a.send();
}
<div id ="output"></div>