0

我已經創建了一個簡單的模型,並彈出一個圖像。圖像來自實時​​相機,我需要每兩秒更新一次。如何在引導模式上附加setInterval

我想要做的是使用setInterval刷新模式內的URL,但我遇到了一些問題。

1當我彈出打開模式窗口時,我似乎無法在html中找到URL src。 2我不確定這是最好的技術,因爲窗戶將繼續打開和關閉。

這是目前爲止的更多代碼。

HTML鏈接

<div class="img camera" id="18"><div class="desc"> 
     City<br>Street @ EXIT</div> <br> <img class="livecamera" src="http://url-i-want-refesh.jpg" border="0"><br> time stamp</div> 

代碼,彈出打開模態

$("#links").on("click", ".camera", function(e){ 
     e.preventDefault() 
     camera = $(this).html(); 
     $('#basicModal').modal('show'); 
     $("#basicModal .modal-body").html(camera); 
    }); 

HTML MODAL

<div class="modal" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true"> 
<div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-body"> 
      <h3>Modal Body</h3> 
     </div> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
     </div> 
    </div> 
</div> 

JAVASCRIPT我試圖獲取工作

$('body').on('shown.bs.modal', '.modal', function (e) { 
    //Get the URL in the Modal 
    url = $(this).find('.livecamera').attr('src'); 
    // refresh it every two seconds 
    setInterval(function(){ 
     $(this).find(".livecamera").attr("src", url+new Date().getTime()); 
     console.log('update'); 
    },2000); 

}) 

我會很感激就如何最好地處理這個任何意見/建議。在此先感謝

回答

0

試試這個:

var interval = null; 
$("#links").on("click", ".camera", function(e){ 
    e.preventDefault(); 
    // get the camera source 
    var camera = $(this).html(); 
    var url = $(this).find('.livecamera').prop('src'); 
    $("#basicModal .modal-body").html(camera); 
    clearInterval(interval); 

    $('#basicModal').modal('show').off('shown.bs.modal').on('shown.bs.modal', function(){ 
    var $this = $(this); 
    interval = setInterval(function(){ 
     $this.find('.livecamera').attr("src", url+new Date().getTime()); 
     console.log('update'); 
    }, 2000); 
    }); 
}); 

看着我的初步建議後,我們多次這很可能是新問題的原因附加的事件偵聽器。我已經更新了代碼,在添加之前刪除了以前的事件偵聽器。

+0

不錯,它現在幾乎工作。非常感謝!我想我試圖以錯誤的方式附加功能。 – Showcaselfloyd 2014-10-01 17:47:13

+0

好的,新的問題。一切工作正常。除了現在它不會清除模態中的以前的HTML。它會讓我點擊兩次,但第一次它會顯示我以前的字符串HTML.I雖然使用此代碼將修復它,但它並沒有辦法。 。 'code' \t \t $( '#basicModal')上( 'hidden.bs.modal',函數(){ \t \t \t變量$這= $(本); \t \t \t $( this).removeData('bs.modal'); \t \t}); 'code' – Showcaselfloyd 2014-10-01 18:09:47

+0

更新了我的答案,希望這可以修復它 – 2014-10-01 18:20:04