-1

I'm trying to update my web page's time every 3 seconds. this is how how i'm trying to do it:

[OutputCache(Duration = 3)]
public ActionResult Index()
{
   ViewBag.Message = "Time : " + DateTime.Now;

   return View();
}

but it does't refresh the page. Can someone please give me an idea how to solve this. thank you in advance!

Dayan
  • 371
  • 1
  • 5
  • 12
  • 1
    it won't update page untill you send a request to your action. Better solution is to use javascript to update time using timer – Dmitry Pavliv Jan 02 '15 at 10:34
  • 1
    Or take a look at SignalR if u would like data to be pushed in your page without you doing a pull – qamar Jan 02 '15 at 10:35
  • can you please give me a link to check out how to do it using javascript – Dayan Jan 02 '15 at 10:35
  • OutputCache is something related to caching the responses temporally at the server and this will not refresh the page. Use JavaScript setTimeout to refresh/update some controls based on some interval. – Thangadurai Jan 02 '15 at 10:37
  • [How to create a JQuery Clock / Timer](http://stackoverflow.com/questions/2604450/how-to-create-a-jquery-clock-timer) – Dmitry Pavliv Jan 02 '15 at 10:38
  • By using Javascript SetInterval method and call url reload by window.location.reload(); – Girish Sakhare Jan 02 '15 at 12:00

2 Answers2

1

You can Achieve it by using java script in asp form.

<p id="demo"></p>

<script>
var myVar=setInterval(function(){myTimer()},1000);

function myTimer() {
    var d = new Date();
    document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>
Dmitry Pavliv
  • 34,126
  • 12
  • 75
  • 77
Jawad Zeb
  • 481
  • 3
  • 17
0

You also may use the meta tag for refreshing the page if it's ok to hit the server as below,

<meta http-equiv="refresh" content="3;">
Venkat
  • 1,148
  • 1
  • 8
  • 17