我使用这个ajax脚本:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.EnumMap;
import java.util.Map;
import javax.swing.*;
@SuppressWarnings("serial")
public class StateDependentMouseListener extends JPanel {
private static final int W = 800;
private static final int H = 650;
private static final double CIRCLE_WIDTH = 60.0;
private ButtonGroup buttonGroup = new ButtonGroup();
private ColorState colorState = null;
private Map<ColorState, Shape> colorStateMap = new EnumMap<>(ColorState.class);
public StateDependentMouseListener() {
setPreferredSize(new Dimension(W, H));
for (final ColorState state : ColorState.values()) {
// create the JRadioButton
JRadioButton radioButton = new JRadioButton(state.getText());
add(radioButton); // add to GUI
buttonGroup.add(radioButton); // add to ButtonGroup
// give it an ActionListener that changes the object's state
radioButton.addActionListener(e -> {
colorState = state;
});
// create a randomly placed Ellipse2D and place into Map:
double x = Math.random() * (W - CIRCLE_WIDTH);
double y = Math.random() * (H - CIRCLE_WIDTH);
Ellipse2D ellipse = new Ellipse2D.Double(x, y, CIRCLE_WIDTH, CIRCLE_WIDTH);
colorStateMap.put(state, ellipse);
}
MyMouse myMouse = new MyMouse();
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// make for smooth graphics
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// iterate through the enum, extracting the ellipse and drawing it
for (ColorState state : ColorState.values()) {
Shape shape = colorStateMap.get(state);
if (shape != null) {
g2.setColor(state.getColor());
g2.fill(shape); // draw the ellipse
}
}
}
private class MyMouse extends MouseAdapter {
private Shape selectedShape = null;
private Point2D offset = null;
@Override
public void mousePressed(MouseEvent e) {
// check that correct button pressed
if (e.getButton() != MouseEvent.BUTTON1) {
return;
}
// has our colorState been set yet? If not, exit
if (colorState == null) {
return;
}
// is an appropriate Shape held by the Map? If so, get it
Shape shape = colorStateMap.get(colorState);
if (shape == null) {
return;
}
// does this shape contain the point where the mouse was pressed?
if (!shape.contains(e.getPoint())) {
return;
}
// Get the selected shape, get the mouse point location relative to this shape
selectedShape = shape;
double x = e.getX() - shape.getBounds2D().getX();
double y = e.getY() - shape.getBounds2D().getY();
offset = new Point2D.Double(x, y);
}
@Override
public void mouseDragged(MouseEvent e) {
// drag shape to new location
if (selectedShape != null) {
double x = e.getX() - offset.getX();
double y = e.getY() - offset.getY();
Rectangle2D bounds = selectedShape.getBounds2D();
bounds.setFrame(new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight()));
((Ellipse2D) selectedShape).setFrame(bounds);
repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
selectedShape = null;
}
}
private static void createAndShowGui() {
StateDependentMouseListener mainPanel = new StateDependentMouseListener();
JFrame frame = new JFrame("StateDependentMouseListener");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
按下按钮后,我创建一个转到edit_id.php的变量,执行某些过程并将结果返回到id =“id”的同一个Div。一切都很完美但是...按下按钮约30-40次后,页面开始工作超慢。我阅读了很多主题,我添加了一些东西,但是我没有太多的Ajax tehnique经验....而且不能单独处理它...如果你帮助我,我将非常感谢你...提前谢谢你!
答案 0 :(得分:1)
好吧,我知道这个问题是很久以前提出的,但以防万一从现在开始有人正在寻找某种答案。我只是想补充一点,我在这个网站上读到了一些东西:https://www.phpclasses.org/blog/post/277-Fix-the-AJAX-Requests-that-Make-PHP-Take-Too-Long-to-Respond.html 所以基本上它教的是一种在你使用 PHP 的情况下优化你的网站的方法
如果您使用的是 PHP 会话,则必须使用函数 session_start()
,这样当您调用它时,PHP 会加载会话文件并将其序列化以通过 $_SESSION
访问变量,但它会使文件被脚本阻止。
因此该网站建议在您完成验证后,使用将释放文件的 session_write_close
。
为什么要这样做的答案是因为当您同时发出 AJAX 请求并且您正在使用会话进行验证或其他任何事情时,服务器将阻止该文件,直到脚本结束,然后将执行下一个请求。
所以我建议验证和发布,如果它们是您以后想要使用的一些数据,请考虑将值保存在普通的 PHP 变量中,而不是每次都使用 $_SESSION
超级数组。
这只是可能会提高您的服务器响应速度的东西,并不打算成为所问问题的绝对答案,我真的希望它有所帮助,并且您会找到最佳编程实践来加速您的网站!< /p>
答案 1 :(得分:0)
而不是从服务器脚本中检索所有表数据:
$('#id').html(data);
尝试将新行数据仅添加到表中,如下所示:
$("#id").append(data);
服务器端脚本必须返回如下值:
<tr><td>test</td><td>test</td></tr>