title: call javascript function with c# methods in razor
body: <p>I am trying to reload a page every 10 seconds ,  using Cache Scheduler method mentioned <a href="https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/">here</a>,</p>
  <p>I just want call a <code>JavaScript</code> function to reload current page when <code>Cache</code> is removed. But for some reason  basic JavaScript doesn't work.</p>
  <p><strong>Razor View:</strong></p>
  <pre><code>@{
  
  AddTask(&quot;CheckStatus&quot;, 10); // reload page every 10 seconds
  
  @functions
  {
  
  private static System.Web.Caching.CacheItemRemovedCallback OnCacheRemove = null;
  
  public void AddTask(string name, int seconds)
  {
  OnCacheRemove = new System.Web.Caching.CacheItemRemovedCallback(CacheItemRemoved);
  HttpRuntime.Cache.Insert(name, seconds, null,
  DateTime.Now.AddSeconds(seconds), System.Web.Caching.Cache.NoSlidingExpiration,
  System.Web.Caching.CacheItemPriority.NotRemovable, OnCacheRemove);
          
  }
  
  
  public void CacheItemRemoved(string k, object v,
  System.Web.Caching.CacheItemRemovedReason r)
  {
          
   PrintStatus();
  
  }
  
  
  }
  
  
  }
  
  
  @helper PrintStatus()
  {
   
   var msg = &quot;&lt;script language='javascript' type='text/javascript'&gt;
   alert('Status is open!');&lt;/script&gt;&quot;;
  
   @Html.Raw(msg)
  
  }
  </code></pre>
  <p>What am i doing wrong ? Or Is there any alternate way to return <code>Javascript</code> to reload page using server side code.</p>
  <p><strong>Note:</strong> I am not using <code>Javascript</code> <code>set interval</code> because <code>Chrome</code> has <a href="https://stackoverflow.com/questions/38376157/javascript-reload-page-every-5-seconds-when-browser-is-minimized">issues</a> when the browser is minimized, so i prefer server side code to reload page every x seconds.</p>
  <p>Any help would be great.</p>