2013-03-30 33 views
-1

我是新來的JQuery和Javascript,並且找不到一個很好的示例如何刷新div中的html對象。JQuery Reload到

的HTML對象是從cosm.com該曲線圖 enter image description here

的index.html

<div id="graph" > 
<object type="text/html" style="width:100%; height:100%; margin:1%;"data="https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading.png?width=740&height=150&colour=%230055ff&duration=5minutes&legend=Vibrations%20measure%20%200%20-%3E%201024&title=Foosball%20Vibrations%20-%205%20Minutes&stroke_size=4&show_axis_labels=true&detailed_grid=true&scale=manual&max=75&timezone=Mountain%20Time%20(US%20%26%20Canada)" > 
</object> 
</div> 

的script.js

$(document).ready(function() { 
updateGraph; 
setInterval (updateGraph, 2000); 
} 
function updateGraph() { 
    //This line is not working!!!! 
    $('#graph').reload(); 
    //$('#graph').load(); 

} 

http://jsfiddle.net/spuder/PNK4k/

+0

有作爲'.reload無此功能()'。您是否嘗試更改源URL(例如,添加「&_rand = 12334234123」)以強制重新加載?另外,當結果是PNG圖像時,爲什麼使用''? – user113215

+0

試試'$('#graph object')attr。('data',url);'其中'url'是適當資源的路徑。 @ user113215的cache-busting'_rand'可能或可能不是必需的,但應用它比不安全更安全。 –

+0

這是關於它是一個PNG的好點。據我所知,你應該可以使用''標籤而不是''。 –

回答

1

嘗試了這一點http://jsfiddle.net/Bvpft/16/

測試Chrome和FireFox。它刷新並重新下載圖像,但不知道除此之外它應該做什麼。

HTML

<!DOCTYPE html> 
<body> 
    <div id="graph" > 
     <object type="text/html" style="width:100%; height:100%; margin:1%;" data=""> 
     </object> 
    </div> 
</body> 

的Javascript

$(document).ready(function() { 
    var graph = $('#graph'), 
     uri = 'https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading.png?', 
     params = { 
      width: 740, 
      height: 150, 
      colour: '#0055ff', 
      duration: '5minutes', 
      legend: 'Vibration measure 0 -> 1024 ', 
      title: 'Foosball Vibrations - 5 Minutes', 
      show_axis_labels: 'true', 
      detailed_grid: 'true', 
      scale: 'manual', 
      max: 75, 
      timezone: 'Mountain Time (US & Canada)' 
     }; 
    var updateGraph = function() { 
     var data = uri + $.param(params); 
     graph.empty(); 
     graph.append($('<object/>') 
        .prop({'type': 'text/html', data: data}) 
        .css({'width': '100%', 'height': '100%'})) 
    }; 
    updateGraph(); 
    setInterval (updateGraph, 1000); 
}); 

編輯 - 改變到圖像時,由非閃爍,不添加緩存(在IE10,鉻和Firefox測試):http://jsfiddle.net/JAYDb/4/

HTML

<div id="graph" ></div> 

CSS

#graph { 
    background-color:#C0C0C0; 
    margin: 0 auto; 
    min-height: 200px ; 
    min-width: 750px ; 
    display: inline-block; 
} 
#graphObject{ 
    display: block; 
    width: 100%; 
} 

的Javascript

$(document).ready(function() { 
    var uri = 'https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading.png?', 
     params = { 
      width: 740, 
      height: 150, 
      colour: '#0055ff', 
      duration: '5minutes', 
      legend: 'Vibration measure 0 -> 1024 ', 
      title: 'Foosball Vibrations - 5 Minutes', 
      show_axis_labels: 'true', 
      detailed_grid: 'true', 
      scale: 'manual', 
      max: 75, 
      timezone: 'Mountain Time (US & Canada)' 
     }; 
    var data = uri + $.param(params), 
     graph = $('#graph'), 
     obGraph = $('<img/>').prop({'id': 'graphObject'}); 
    var updateGraph = function() { 
     var date = new Date(), 
     src = data + '&' + date.getTime(); 
     obGraph.prop('src', ''); 
     obGraph.prop('src', src); 
    }; 
    graph.empty().append(obGraph); 
    updateGraph(); 
    setInterval (updateGraph, 1000); 
}); 
+0

這正是我所期待的。非常感謝。在safari中也很棒。 – spuder

0

右括號缺失?

$(document).ready 
( function() 
    { 
    updateGraph; 
    setInterval (updateGraph, 2000); 
    } 
); // missing 

你是jsfiddle不工作,因爲沒有選擇jQuery版本。我做了一個工作,例如:?

$(document).ready(function() { 
    alert("OK"); 
    updateGraph(); 
    window.setInterval(updateGraph, 5000); 
}); 

function updateGraph() { 
    //This line is not working!!!! 
    alert("ok"); 
    //$('graph').reload(); // unknown method? 
} 

http://jsfiddle.net/mmGT9/4/

你是什麼意思與「$( '曲線')的重載();那是一個自定義函數

如果你?要重新加載「對象」,我會從HTML區域去除異物,只留下空白格,然後加上下面一行在你的JavaScript功能。

$("#graph").html('<object type="text/html" style="width:100%; height:100%; margin:1%; " data="https://api.cosm.com/v2/feeds/120687/datastreams/sensor_reading.png?width=740&height=150&colour=%230055ff&duration=5minutes&legend=Vibrations%20measure%20%200%20-%3E%201024&title=Foosball%20Vibrations%20-%205%20Minutes&stroke_size=4&show_axis_labels=true&detailed_grid=true&scale=manual&max=75&timezone=Mountain%20Time%20(US%20%26%20Canada)" ></object>'); 

這將設置objec t放入ID爲「graph」的元素中。 div內的現有內容將被覆蓋。該函數每隔n秒調用一次,並替換該對象,這會強制重新加載。你可以通過使用變量來更漂亮。

下面是一個例子:http://jsfiddle.net/mmGT9/5/

+0

您錯過了添加jQuery後保存小提琴。 –

+0

這不是問題的答案 –

0

jsFiddle

該工程..

我改變了你的objectimg標籤

<img id='obj' style="width:100%; height:100%; margin:1%; " src=... 


$('document').ready(function() { 
    var i; 

     var src = $('#obj').prop('src'); 
    setInterval(function(){ 
     i++; 
     $('#obj').prop('src',src+"&"+i); 

    },5000); 
});