如何使用javascript每20秒更新一次选取内容? 我的意思是,在前20秒他读取第一个对象,在第二个40读取第二个对象,依此类推......,而不更新页面,意味着自动更新它 这是javascript函数:
function read () {
$.getJSON("jsons/jobs.json", function(json) {
Thejobs = json;
for(var k in Thejobs.Studies){
$('#boxContent').append('<br>'+"position: "+Thejobs.Studies[k].position+
'<br>'+"Academy: "+Thejobs.Studies[k].academy+'<br>'+"address: "
+Thejobs.Studies[k].address+'<br>'+"Description: "+Thejobs.Studies[k].jobDescription)
$('#boxContent').append("<br>_____________________");
}}); }
这是我的json文件:
{"Studies": {
"jobID1": {
"position": "student position",
"academy": "Haifa University",
"address": "haifa,isreal",
"jobDescription": "Big data"
},
"jobID2": {
"position": "Research 1",
"academy": "saarland University",
"address": "saarbrucken , germany",
"jobDescription": "Electronic engineer"
},
"jobID3": {
"position": "Studie 1",
"academy": "Technion",
"address": "haifa,isreal",
"jobDescription": "Speed of internet"
},
"jobID4": {
"position": "Studie 2",
"academy": "Technion",
"address": "USA ,los angeles",
"jobDescription": "analysis data "
}
}
}
html文件:
<marquee direction="up" scrollamount="2">
<p id="boxContent"></p>
</marquee>
我试过这个但是没有工作:
<script>
var myVar = setInterval(joobs(), 20000);
var ii=0;
function joobs(){
$.getJSON("jsons/jobs.json", function(json) {
Thejobs = json;
$('#boxContent').append('<br>'+"position:
"+Thejobs.Studies[ii].position+
'<br>'+"Academy: "+Thejobs.Studies[ii].academy+'<br>'+"address: "
+Thejobs.Studies[ii].address+'<br>'+"Description:
"+Thejobs.Studies[ii].jobDescription)
$('#boxContent').append("<br>_____________________");
});
ii=ii+1;
</script>
答案 0 :(得分:1)
我无法抗拒使这种憎恶。
如果您需要定期执行某些操作,请使用setInterval
。哪个具有签名**Loop the values**:
BigDecimal.add(BigDecimal.get(List.get(int index)));
**Delete the amount of times that the loop ran:**
BigDecimal.remove(int index);
只需将您的函数更改为setInterval(callback_function, interval_in_milliseconds);
的值作为回调,并将间隔设置为marquee
以延迟20秒。您的功能将每20秒调用一次,如下例所示。
20000
var values = ["Marquees", "shall", "never", "die.", "Long", "live", "the", "marquee!"];
var iterator = 1;
var colorator = 0;
function updateMarquee() {
document.getElementById('boxContent').innerHTML = values[iterator];
iterator = (iterator + 1) % values.length;
console.log("updateMarquee() has been called");
return function() {
console.log("updateMarquee() return value was called instead of the function");
};
}
setInterval(updateMarquee, 1000);
setInterval(updateMarquee(), 10000);
更新:关于user78403的回答,我更新了我的代码段,以显示在<marquee direction="right" scrollamount="10" behavior="alternate">
<p id="boxContent">Marquees</p>
</marquee>
中调用函数本身和调用函数返回值之间的区别。在示例中,由于行setInterval
,您将看到updateMarquee() has been called
每秒都会记录一次。但是,由于行setInterval(updateMarquee, 1000);
updateMarquee() return value was called instead of the function
P.S。不要使用选框。 20年前,在网站上进行任何自动移动,播放和更改都可能令人费解,但现在它只是令人烦恼,用户将看不起您的网站。您应该拥有一份用户可以按自己的节奏阅读的工作机会的静态列表。
答案 1 :(得分:1)
我更正了代码中的一些语法错误。我将解释我所做过的最重要的事情:
setInterval(joobs(), 20000)
- &gt; setInterval(joobs, 20000)
:你必须这样做
将方法本身作为参数传递。否则将调用该方法
并且它的返回值将每20秒执行一次。Thejobs.Studies[ii]
- &gt; Thejobs.Studies["jobID" + ii]
:将属性名称作为索引。传递号码不起作用。由于JSON文件的属性名称,还必须使用ii
而不是1
初始化0
。 HTML和JSON没有改变。 所以这一切看起来像这样:
var myVar = setInterval(joobs, 20000);
var ii=1;
function joobs() {
$.getJSON("jsons/jobs.json", function (json) {
var Thejobs = json;
$('#boxContent').append('<br>' + "position:" +
Thejobs.Studies["jobID" + ii].position +
'<br>' +
"Academy: " +
Thejobs.Studies["jobID" + ii].academy +
'<br>' +
"address: " +
Thejobs.Studies["jobID" + ii].address +
'<br>' +
"Description:" +
Thejobs.Studies["jobID" + ii].jobDescription);
$('#boxContent').append("<br>_____________________");
});
ii = ii + 1;
}