简体   繁体   English

如何在不使用外部php文件的情况下执行ajax(jquery)功能

[英]How to perform ajax (jquery) functionality without using an external php file

Current scenario: 当前场景:

I'm using the gmail oauth api to receive emails on a page. 我正在使用gmail oauth api来接收页面上的电子邮件。 It's slow to load many, so I want to post each email on the page as it loads giving the user a chance to do other things on the site while the emails are still loading. 加载速度很慢,因此我想在加载时在页面上发布每封电子邮件,从而使用户有机会在仍加载电子邮件的同时在网站上执行其他操作。

There are a few files required 需要一些文件

require_once 'common.php';
require_once 'Zend/Oauth/Consumer.php';
require_once 'Zend/Crypt/Rsa/Key/Private.php'; 
require_once 'Zend/Mail/Protocol/Imap.php';
require_once 'Zend/Mail/Storage/Imap.php';
require_once 'Zend/Mail.php';

And a few functions on the main page that helps the php run. 和主页上的一些功能可以帮助php运行。 I am familiar with using the ajax call on jquery to call an external php file. 我熟悉在jquery上使用ajax调用来调用外部php文件。 I would like to be able instantiate php code on this page to function using ajax functionality so I don't need to worry about calling these required files and functions each time I check a new email. 我希望能够在此页面上实例化php代码以使用Ajax功能运行,因此我不必担心每次查看新电子邮件时都会调用这些必需的文件和功能。 Is there a way to do this? 有没有办法做到这一点?

  for ($i = $storage->countMessages(); $i >= ($storage->countMessages()-30); $i-- ){ 
 { 
  echo '<li>' . $storage->getMessage($i)->subject . '</li>'; 
 }

  } 

Is the function I would like to function on the fly and return each subject one at a time to load on the screen. 是我想即时运行的功能,并且一次返回每个主题一次以加载到屏幕上。 I assume I'll need to create a for loop using javascript, but the main issue is being able to use the php on the page so that I don't have to recall the includes each time. 我假设我需要使用javascript创建一个for循环,但是主要的问题是能够使用页面上的php,这样我就不必每次都调用include了。 Maybe I'm curious about changing the scope of these variables, maybe the solution is being able to operate the ajax from this page without an external script - I'm not sure, but any help would be appreciated. 也许我对更改这些变量的范围感到好奇,也许解决方案是能够在没有外部脚本的情况下从此页面操作ajax-我不确定,但是可以提供任何帮助。

Firstly, don't worry about the performance of 6 includes. 首先,不要担心6个include的性能。 This something you start to worry about when your site is getting massive. 当您的网站规模庞大时,您就开始担心这一点。 Having said that, utilizing an opcode cache such as APC can reduce the performance cost of including large numbers of files. 话虽如此,利用操作码缓存(例如APC)可以降低包含大量文件的性能成本。

To your question: every request to PHP is it's own entity and cannot reference code included in another request. 给您的问题:对PHP的每个请求都是它自己的实体,不能引用另一个请求中包含的代码。 Your original request and each subsequent AJAX request are all separate and distinct requests that know nothing of each other and will all require PHP to load all files to load emails. 您的原始请求和每个后续的AJAX请求都是相互独立的请求,彼此之间一无所知,并且都需要PHP加载所有文件才能加载电子邮件。

I suggest you look at other ways to improve performance. 我建议您考虑其他提高性能的方法。 Given loading emails from gmail is slow, perhaps you could investigate using a daemon (a service that runs continually in the background on your server) that synchronizes the emails into a database. 鉴于从gmail加载电子邮件的速度很慢,也许您可​​以使用将电子邮件同步到数据库的守护程序(在服务器后台连续运行的服务)进行调查。 Your page can then just get everything it needs from the database which will be much faster for the user. 这样,您的页面就可以从数据库中获取所需的一切,这对于用户而言将更快。 You can use AJAX to check the database periodically for new data. 您可以使用AJAX定期检查数据库中的新数据。 Meanwhile your daemon will be doing the hard-work in the background. 同时,您的守护程序将在后台进行艰苦的工作。

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

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