简体   繁体   English

MVC在不可预知的时间从razor @Helper调用Javascript

[英]MVC call Javascript from razor @Helper at unpredictable times

In my MVC view I need to get razor c# code to execute a javascript function at unpredictable times, way after the page has loaded. 在我的MVC视图中,我需要获取razor c#代码以在页面加载后的不可预测的时间执行javascript函数。

I have used a thread to simulate unpredictableness but ultimately instead of the thread it will be a WCF callback method that raises an event which runs the helper, but to eliminate session issues I have used the thread. 我使用了一个线程来模拟不可预测性,但是最终将代替WThread而是使用WCF回调方法,该方法引发一个运行帮助程序的事件,但为了消除会话问题,我使用了该线程。

Javascript to execute: 要执行的Javascript:

<script type="text/javascript">
    function DisplayNews(news) {
        alert(news);
    }
</script>

Helper that runs the javascript (because sticking this directly in the below thread didn't work) 运行javascript的辅助程序 (因为将其直接粘贴在下面的线程中不起作用)

@helper UpdateNews(string news)
{          
    <script>
        DisplayNews(news);
    </script>   
}

Thread that simulates unpredictableness/post page loading or non user invoked events 模拟不可预测性/后页面加载或非用户调用事件的线程

@{
    System.Threading.Thread T = new System.Threading.Thread(new System.Threading.ThreadStart(delegate
    {
        while (true)
        {
            System.Threading.Thread.Sleep(5000);
            UpdateNews("Some cool news");        
        }
    }));

    T.Start();
}

If I stick a break point at UpdateNews("Some cool news"); 如果我在UpdateNews("Some cool news");UpdateNews("Some cool news");一个断点UpdateNews("Some cool news"); I can see that it gets hit every 5 seconds as it should, but thats as far as it gets, nothing else happens. 我可以看到它每5秒就会受到打击,但是就它获得的结果而言,什么也没有发生。 I can't stick a break point in the helper or the Javascript so I can't see where it stops working after that. 我无法在辅助程序或Javascript中添加断点,因此之后看不到它在哪里停止工作。

Is this going to work at all or is there another way I should be approaching this? 这是行得通吗,还是我应该采用另一种方式来解决这个问题?

Any help would be greatly appreciated. 任何帮助将不胜感激。

In server side code you can call an client function... 在服务器端代码中,您可以调用客户端函数...

Razor executed in server side and javascript is in client side.that mean when you get server response it's created by razor code in server side and now you can just use javascript in client side Razor在服务器端执行,而javascript在客户端。这意味着当您获得服务器响应时,它是由服务器端的razor代码创建的,现在您可以在客户端使用javascript

I may be misunderstanding what you are trying to do but you can have javascript that will run on page load that will be wrapped in a set timeout with a random millisecond period. 我可能会误解您要尝试执行的操作,但是您可以让javascript在页面加载时运行,并将其包装在具有随机毫秒周期的设置超时中。

Something like this: 像这样:

<script>
  window.onload = function() {
     setTimeout(function() {}, 1000); // this will run 1 second after page load
  }
</script>

simply randomize the number being passed as the second parameter to setTimeout and your javascript will run at a random time after the page loads. 只需将要传递的数字随机化为setTimeout的第二个参数,您的JavaScript就会在页面加载后的随机时间运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM