我試圖創建一個倒數計時器,並由於某種原因,使得每個這些倒計時的類獨特。我所有的倒計時都是相同的,儘管它們都是不同的due_dates。列表倒計時計時器列表給每個列表項相同的計時器結果軌道
在我的console.log(reservation.duedate)我得到不同的due_dates這是正確的,它是假設。
倒計時對於js中的倒計數函數中的每個列表都使用due_date的第一個結果。這是不正確的;每個列表項目的每個倒計時必須是分開的。
這是your_essays方法在reservations_controller:
@reservations_pending = user.reservations.where("turned_in = ?", false).where("completed = ?", false).where("due_date >= ?", DateTime.now).order(created_at: :desc).page(params[:page_pending]).per_page(3)
your_essays.html.erb:
<div id="content-load-more-pending">
<%= render 'your_essays_pending', reservations_pending: @reservations_pending %>
</div>
這是your_essays.html.erb內的部分稱爲_your_essays_completed.html.erb
<% reservations_pending.each do |reservation| %>
<div style="color:blue;" class="countdown-class"></div>
<script type="text/javascript">
$(".countdown-class")
.countdown("<%= reservation.due_date %>", function(event) {
$(this).text(
event.strftime('%D days %H:%M:%S')
);
});
console.log("<%= reservation.due_date %>");
</script>
<% end %>
這裏CONSOLE.LOG(事件)和的console.log(」 <%=保留。 due_date%>「);
實際上是被傳遞給javascript函數是什麼事件? (你可以console.log嗎?) –
你的實現有一個錯誤。您正在更新每行中當前數據的整個類別......這不是您應該如何執行的。 –
我在問題中添加了console.log(event)圖片以顯示我在控制檯中看到的內容。事件的第一個日期不同,然後所有的日期都顯示相同的日期。 –