0

I am coding a MVC 5 internet application, and would like to know how to perform a calculation in a view every second.

Here is my view code:

var expires = Model.account.subscriptionEndDate - DateTime.UtcNow;

How can I calculate the expires value every second so that I can display this value? The Model.account.subscriptionEndDate is a DateTime in UTC.

Thanks in advance.

Simon
  • 7,991
  • 21
  • 83
  • 163
  • You need javascript. [Example of using the setTimeout method here](http://www.plus2net.com/javascript_tutorial/clock.php) –  Jan 28 '15 at 07:15

1 Answers1

0

javascript:

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

function myTimer() {
    var d = new Date();
    document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
Florian Schmidinger
  • 4,682
  • 2
  • 16
  • 28
  • Is this possible to do using MVC View code helpers, rather than in a javascript function? – Simon Jan 28 '15 at 08:19
  • to be honest: no clue. on the client side you have to use javascript so the helper will eventually produce javascript. but what you can definatly do is getting data inside this function using razor(given that you have the script on your page/partial and not in a seperate file) – Florian Schmidinger Jan 28 '15 at 08:23
  • @user3736648 Maybe you should try SignalR or XSockets.NET libraries in order to create live page if you don't want JS. – MacGyver Jan 28 '15 at 08:32
  • Of course you can't use helpers. Your view is generated on the server and then sent to the client browser. After that, the only way to manipulate the view is using javascript running on the client (or submitting a form) –  Jan 28 '15 at 09:41