简体   繁体   English

如何在Jquery中的后台运行代码块

[英]How to run a block of code in background in Jquery

I am building a Mobile Application using Jquery Mobile and PhoneGap. 我正在使用Jquery Mobile和PhoneGap构建移动应用程序。 I am using a Single Page Model , whereby I have something like index.html, login.html etc 我正在使用单页模型 ,我有类似index.html,login.html等

I have written a javascript that always contact the server side for new messages every 5 minutes using setInterval() and all works well in background without interfering with what the user is doing. 我编写了一个javascript,每隔5分钟就会使用setInterval()与服务器端联系以获取新消息,并且所有这些都可以在后台运行良好,而不会干扰用户正在做的事情。

I want to ask if it is possible to declare and run this block of code globally instead of having to re-declare it in every html page. 我想问一下是否可以在全局范围内声明并运行这段代码,而不必在每个html页面中重新声明它。

Note: I am using rel="external" in the links in jQuery Mobile which causes each page to refresh when a user navigate to a page. 注意:我在jQuery Mobile的链接中使用rel="external" ,这会导致每个页面在用户导航到页面时刷新。

我相信你主要担心的是当另一个脚本被加载时它会覆盖前一个计时器,所以如果剩下一分钟直到下一次调用,现在有5个。为了克服这个障碍你可以按顺序添加一种逻辑到你的间隔在一天中的特定时间运行,例如9.00,9.05,9.10 ...这样,每次执行脚本时都应该清除InterInterval并重置它,以便下次执行代码时是针对浏览器计算的标准时间约会时间。

I guess u can use web workers for this purpose, they work within a global context and independently , like threads , and they are really efficient which allows you to even perform I/O. 我想你可以使用web worker来实现这个目的,它们可以在全局环境中独立工作,就像线程一样,它们非常高效,甚至可以让你执行I / O.

Please check this link, Hope this helps you 请检查此链接,希望这可以帮助您

https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers

https://groups.google.com/forum/#!topic/phonegap/SMMEwYXUjCU https://groups.google.com/forum/#!topic/phonegap/SMMEwYXUjCU

It will be more feasible if you use the concept of MVC in your project . 如果在项目中使用MVC的概念,那将更加可行。 Try to make a base class which is used by the other classes . 尝试创建其他类使用的基类。 you can use the concept of jquery widget too 你也可以使用jquery widget的概念

I think this should work... 我认为这应该有效......

  1. Creating a page (master page) that its only job is to run a timer... 创建一个页面(母版页),它唯一的工作就是运行一个计时器......
  2. On the master page add an iframe (set to 100% visibility) and point the iframe to your other pages... 在母版页上添加iframe(设置为100%可见性)并将iframe指向其他页面...

What this will do, is that everytime one of your pages (in the iframe) postback, the master page will be kept in memory and the timer will not get reloaded... 这将是什么,是每次你的一个页面(在iframe中)回发,主页面将保留在内存中,计时器将不会重新加载...

Sample master page. 示例母版页。

<html>
 <head>
  <script>
   function checkMessages() {
    // check messages here
    setInterval(...);
    }
  </script>
 </head>
 <body onload="checkMessages();">
     <iframe id="iframe1" src="subpage.aspx" width="100%" scrolling="auto" frameborder="no" marginheight="0" marginwidth="0" height="100%"></iframe>
 </body>
</html>

I was able to use Android Background Service to achieve what I wanted. 我能够使用Android后台服务来实现我想要的。 I used a plugin with PhoneGap. 我在PhoneGap上使用了一个插件。 For those who want to use similar technique, you can get the info about the plugin on phonegap-service-tutorial-part-1 对于那些想要使用类似技术的人,你可以在phonegap-service-tutorial-part-1上获得有关插件的信息

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

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