简体   繁体   English

循环JSON内容而无需重新加载页面

[英]Looping over contents of a JSON without reloading the page

I have a JSON array(?) of pairs of every state and a value associated with that state, it looks like the following below: 我有每个状态对和与该状态相关联的值的成对的JSON数组(?),它看起来如下所示:

var states = [{"STATE":"AL","AMOUNT":"6"}, {"STATE":"AK","AMOUNT":"3"}]

I need the page to shuffle through them without reloading the page 我需要页面在不重新加载页面的情况下随机播放它们

"AL 6" [wait x seconds] then "AK 3" [wait x seconds] then etc... “ AL 6” [等待x秒],然后“ AK 3” [等待x秒],然后依此类推...

I need this to run continuously. 我需要它连续运行。

I never use any of these languages but was told that they were my best bet. 我从不使用任何一种语言,但被告知这是我最好的选择。

Could someone give me some guidance on this please. 有人可以给我一些指导吗?

Thank you in advance. 先感谢您。

Alright, you'd need a function that does the rotation through the array, and a variable for keeping the current state (in both meanings of the word): 好了,您需要一个函数来遍历数组,并需要一个变量来保持当前状态(在单词的两个含义中):

var stateIndex = 0;

function rotate() {
    stateIndex++;

    if(stateIndex >= states.length)
        stateIndex = 0;

    displayState(states[stateIndex]);
}

And you'd need an interval to perform the rotation: 并且您需要一个间隔来执行轮换:

var stateRotation = window.setInterval(rotate, 3000); // 3000ms = 3 sec

The stateRotation variable is an identifier of your interval. stateRotation变量是您的时间间隔的标识符。 You may use that if you ever want to stop: window.clearInterval(stateRotation); 如果您想停止,可以使用它: window.clearInterval(stateRotation);

Now, the above code anticipates a function displayState which takes a state object and displays it. 现在,上面的代码期望一个函数displayState ,它接受一个状态对象并显示它。 How that would look depends entirely on how you want your state to displayed. 外观将完全取决于您希望状态如何显示。 In its simplest form, something like this: 最简单的形式如下:

function displayState(state) {
    $('#state-name').html(state.STATE);
    $('#state-amount').html(state.AMOUNT);
}

As per your description, it might perhaps be something more like 根据您的描述,可能更像是

$('#state-text').html(state.STATE + ' ' + state.AMOUNT);

Here's a jsfiddle with setInterval execting a function that alternates between each state and displays it in a div: 这是一个带setInterval的jsfiddle执行一个在每个状态之间交替并在div中显示它的函数:

http://jsfiddle.net/WD5Qj/1/ http://jsfiddle.net/WD5Qj/1/

var states = '[{"STATE":"AL","AMOUNT":"6"}, {"STATE":"AK","AMOUNT":"3"}]';

json = jQuery.parseJSON(states);



var i = 0;

var cycle = function(){
  $("#state").html(json[i].STATE + json[i].AMOUNT); 
  i = (i+1)%json.length;
}

var loop = setInterval(cycle, 500);
var states = [{"STATE":"AL","AMOUNT":"6"}, {"STATE":"AK","AMOUNT":"3"}]; 
var i = 0; 
setInterval(function(){ 
  var array_index = i % states.length; 
  $('#state').html( states[ array_index ]['STATE'] );
  $('#state').html( states[ array_index ]['AMOUNT'] );
  i++; 
}, 2000); 

Here's a fiddle . 这是一个小提琴

function displayNextItem(index){
    if (index === states.length) 
         displayNextItem(0);

    $("#someDiv").text(states[index]["STATE"] + " " + states[index]["AMOUNT"]);
    setTimeout(function() { displayNextItem(index + 1); }, 1000);
}

And then 接着

displayNextItem(0);
var i = 0, l = states.length, timer, intervalLength = 5000;
timer = setInterval(function(){
  if(i >= l){
    clearInterval(timer);
  }else{
    alert(states[i++].STATE);
  }
},intervalLength);

This implementation is waiting the AMOUNT number of seconds. 此实现正在等待AMOUNT秒。 If you want constant number of seconds then other answers are better :). 如果您想要固定的秒数,那么其他答案会更好:)。

JavaScript: JavaScript的:

var states = [{"STATE":"AL","AMOUNT":"6"}, {"STATE":"AK","AMOUNT":"3"}];

function iterate(index) {
  var time = states[index].AMOUNT;

  // replace the text with new one
  $("#output").text(states[index].STATE + " " + time);

  setTimeout(function() {
    var next = (index + 1) % states.length;
    iterate(next);
  }, time * 1000);
}

iterate(0);

HERE is the code. 这里是代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM