我有2个控制器,都使用相同的更新方法来获取数据:
angular.module('project')
.controller('mainController', function($http, $scope, $timeout, $sce) {
updateData($http, $scope, $timeout, $sce, false);
})
.controller('settingsController', function($http, $scope, $timeout, $sce) {
updateData($http, $scope, $timeout, $sce, true);
})
我的updateData
看起来像这样:
function updateData($http, $scope, $timeout, $sce, settings) {
$timeout(function() {
if (settings) {
getSettings($http, $scope);
}
else {
getDataA($http, $scope);
getDataB($http, $scope);
}
updateData($http, $scope, $timeout, $sce, settings);
}, 1000);
}
现在刷新主页面时(使用mainController
),我总是得到“#”;消息,因为进程没有完成所有方法调用,并且当切换到另一个站点时(使用settingsController
),加载它需要很长时间,因为必须首先完成前一个请求。我如何直接"杀死"刷新/切换网站时所有挂起的更新?
答案 0 :(得分:3)
这种做法是错误的。我认为将依赖项传递给open函数并使用它们并不是一个好主意。这可能会产生难以检测的错误。
您的updateData
功能是资源杀手。它每秒都会重复出现而没有限制或控制。
您的代码中存在设计问题。您应该重新考虑以标准有效的方式进行设计。
您的问题的答案是$timeout
返回一个承诺。您可以使用$ timeout.cancel方法取消超时。
var timeoutPromise = $timeout(foo(), 1000);
$timeour.cancel(timoeoutPromise);
答案 1 :(得分:0)
你应该提供服务而不是功能。然后你需要将服务注入控制器。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.Scrollable;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BoxView;
import javax.swing.text.ComponentView;
import javax.swing.text.Element;
import javax.swing.text.IconView;
import javax.swing.text.LabelView;
import javax.swing.text.ParagraphView;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;
public class ScrollExample extends JPanel {
public ScrollExample() {
super(new BorderLayout());
JTextPane textPane1 = new JTextPane();
textPane1.setEditorKit(new WrapEditorKit());
JTextPane textPane2 = new JTextPane();
textPane2.setEditorKit(new WrapEditorKit());
JScrollPane scrollPaneText1 = new JScrollPane(textPane1);
scrollPaneText1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPaneText1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JScrollPane scrollPaneText2 = new JScrollPane(textPane2);
scrollPaneText2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPaneText2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JPanel panel = new RestrictedPanel();
panel.setLayout(new BorderLayout());
panel.add(scrollPaneText2, BorderLayout.CENTER);
panel.add(new JButton("Example"), BorderLayout.NORTH);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, scrollPaneText1, panel);
splitPane.setDividerLocation(100);
add(splitPane);
textPane1.setText("ThisIsAVeryLongStringWhichRepeatsItselfThisIsAVeryLongStringWhichRepeatsItself ThisIsAVeryLongStringWhichRepeatsItself");
textPane2.setText("ThisIsAVeryLongStringWhichRepeatsItselfThisIsAVeryLongStringWhichRepeatsItself ThisIsAVeryLongStringWhichRepeatsItself");
}
public class RestrictedPanel extends JPanel implements Scrollable {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(600, 200);
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return true;
}
}
public static void main(String[] args) {
ScrollExample example = new ScrollExample();
JFrame frame = new JFrame("Example");
frame.setLayout(new BorderLayout());
frame.add(example, BorderLayout.CENTER);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
frame.setBounds(100, 50, 600, 400);
frame.setVisible(true);
}
});
}
public class WrapLabelView extends LabelView {
public WrapLabelView(Element elem) {
super(elem);
}
@Override
public float getMinimumSpan(int axis) {
switch (axis) {
case View.X_AXIS:
return 0;
case View.Y_AXIS:
return super.getMinimumSpan(axis);
default:
throw new IllegalArgumentException("Invalid axis: " + axis);
}
}
}
public class WrapEditorKit extends StyledEditorKit {
protected ViewFactory _factory = new WrapColumnFactory();
@Override
public ViewFactory getViewFactory() {
return _factory;
}
}
public class WrapColumnFactory implements ViewFactory {
@Override
public View create(Element elem) {
switch (elem.getName()) {
case AbstractDocument.ContentElementName:
return new WrapLabelView(elem);
case AbstractDocument.ParagraphElementName:
return new ParagraphView(elem);
case AbstractDocument.SectionElementName:
return new BoxView(elem, View.Y_AXIS);
case StyleConstants.ComponentElementName:
return new ComponentView(elem);
case StyleConstants.IconElementName:
return new IconView(elem);
}
return new LabelView(elem);
}
}
}
然后注入:
project.service('updateService', function(){
...some logic here...
});
当你写一个控制器,一个小技巧时,写下这样:
angular.module('project')
.controller('mainController', function($http, $scope, $timeout, $sce, updateService) {
...use "updateService" methods here...
})
这对于您希望制作代码的缩小版本非常重要。