Click here to Skip to main content
15,891,647 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Everybody

I use from a repeater for get last 3 news from database.
I want show only the first news in page load and after 3 second I want show second news instead of first news and after 3 second I want show third news instead of second news and after 3 second I want show first news instead of third news and ...

I implement bellow code that show only one of the news randomly. Please edit my java script for doing this action.

ASP.NET
<div id="RightContext">
    <asp:SqlDataSource runat="server" ID="DataSource"
        ConnectionString="<%$ConnectionStrings:Database %>"
        SelectCommand="GetNews"
        SelectCommandType="StoredProcedure"></asp:SqlDataSource>
 
    <asp:Repeater runat="server" ID="Repeater" DataSourceID="DataSource">
        <ItemTemplate>
            <div class="News-Ticker" id="TickerID">
                <h3><%#Eval("Title") %></h3>
                <span>Reference:<%#Eval("Reference") %></span>
                <br />
                <span>Date:<%#Eval("IssueDate") %></span>
                                        
            </div>
        </ItemTemplate>
    </asp:Repeater>
 
    <script type="text/javascript">
 
        var News = document.getElementById("RightContext");
        var current = Math.floor(Math.random() * 3);
 
        for (var i = 2; i >= 0; i--) {
            if (i != current) {
                News.children[i].parentNode.removeChild(News.children[i]);
            }
        }
 
    </script>
 
</div>
Posted
Updated 3-May-13 2:28am
v2
Comments
Shubhashish_Mandal 3-May-13 7:19am    
The above logic is to display only one message choose by random. So there is no logic exist to display the message as you expect.
Reza Alipour Fard 3-May-13 8:29am    
I remove 2 news of 3 news therefore exist one news and page will be show that news.
I want change this news after some second without user action.
Akinmade Bond 3-May-13 10:42am    
The current variable randomizes a number. You should use that variable to keep the article(news) that is currently displayed and use it to change to a new article after three seconds.
Reza Alipour Fard 3-May-13 12:21pm    
news was fetched from DB therefore I can fetched that in Java script. If you can do this please give me more information how I can use from a variable in java script that was fetched from data base?
sri senthil kumar 4-May-13 13:18pm    
You want to show news ticker which automatically changing for a specific time period? If thats the case then I have given my solution below check whether it is working.

1 solution

Try the below logic. Hope it helps you, instead of the multiple divs with class 'News-Ticker' use repeater. Thats it. Post your feedback whether it works or not, so that we can fix'em up.

HTML
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
var CurrentIndex = 0;
var TotalCount = 0;
$(document).ready(function() {
 showNewsTicker();	
 TotalCount = $('.News-Ticker').size();
});

function showNewsTicker()
{
 if(CurrentIndex < TotalCount)
 {
  $('.News-Ticker').hide();
  var cb = $('.News-Ticker')[CurrentIndex];
  $(cb).show();
  CurrentIndex = CurrentIndex +1;
 }
 else
 {
  CurrentIndex = 0;
 }
 setTimeout(function(){ showNewsTicker();}, 3000)
}
</script>
</head>
<body>

<div id="RightContext">

<div class="News-Ticker">
NEWS 1
</div>

<div class="News-Ticker">
NEWS 2
</div>

<div class="News-Ticker">
NEWS 3
</div>

</div>

</body>
</html>
 
Share this answer
 
v2
Comments
Reza Alipour Fard 4-May-13 23:25pm    
Your code is OK, But have a little problem: In first load page will show all news and after 3 second your code will work correctly.
Reza Alipour Fard 4-May-13 23:59pm    
I edit it. Thanks a lot.
sri senthil kumar 6-May-13 1:50am    
oh!!! k, you're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900