Javascript onunload函数

时间:2016-05-09 16:40:26

标签: javascript meteor

我在这方面看了很多,但我仍然无法理解。 当用户离开我的页面时,我必须做一些工作。所以我使用window.onbeforeunload警告他他将退出页面和window.onunload来启动我的工作。 问题是,当我正在执行onunload功能时,在我的页面已经关闭之前它没有正确完成。

我试过这个:

        window.onunload = function() {

            //freezeScreen(5000);

            console.log("destroy finished 1/8");

            $.each(_tabClassObj, function(index) {
                _tabClassObj[index].destroy();
            });
            console.log("destroy finished 2/8");
            _tabClassObj = {};
            console.log("destroy finished 3/8");
            _terminalTabCounter = 0;
            console.log("destroy finished 4/8");
            window.onbeforeunload = null;
            console.log("destroy finished 5/8");
            window.onunload = null;
            console.log("destroy finished 6/8");
        }

这是我的控制台日志:

destroy finished 1/8
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.

我试图延迟浏览器用户界面关闭,冻结功能会延迟我的浏览器关闭5秒......但它仍然无法正常工作。我在这里尝试了一个setTimeout ......但结果相同。 问题是什么 ? 谢谢你的建议。

2 个答案:

答案 0 :(得分:0)

如果您使用onDestroy作为视图图层,则可以使用Template.xxx.onDestroy( function() { // logic goes here. }); 方法访问您想要在放弃网址之前添加的逻辑。

var replyText = text.split("\n"); // "\n" is new line character

答案 1 :(得分:0)

我无法用javascript事件做我想做的事。 提到我的meteor服务器,我的客户端关闭了他的浏览器,我开发了一个像这样的ping系统:

@Service
public class EntityConverter {

    @Autowired
    private MappingContext<?, ?> mappingContext;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired(required = false)
    private List<RepositoryRestConfigurer> configurers = Collections.emptyList();

    public <T> T convert(Link link, Class<T> target) {

        DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();

        PersistentEntities entities = new PersistentEntities(Arrays.asList(mappingContext));
        UriToEntityConverter converter = new UriToEntityConverter(entities, conversionService);
        conversionService.addConverter(converter);
        addFormatters(conversionService);
        for (RepositoryRestConfigurer configurer : configurers) {
            configurer.configureConversionService(conversionService);
        }

        URI uri = convert(link);
        T object = target.cast(conversionService.convert(uri, TypeDescriptor.valueOf(target)));
        if (object == null) {
            throw new IllegalArgumentException(String.format("%s '%s' was not found.", target.getSimpleName(), uri));
        }
        return object;
    }

    private URI convert(Link link) {
        try {
            return new URI(link.getHref());
        } catch (Exception e) {
            throw new IllegalArgumentException("URI from link is invalid", e);
        }
    }

    private void addFormatters(FormatterRegistry registry) {

        registry.addFormatter(DistanceFormatter.INSTANCE);
        registry.addFormatter(PointFormatter.INSTANCE);

        if (!(registry instanceof FormattingConversionService)) {
            return;
        }

        FormattingConversionService conversionService = (FormattingConversionService) registry;

        DomainClassConverter<FormattingConversionService> converter = new DomainClassConverter<FormattingConversionService>(
                conversionService);
        converter.setApplicationContext(applicationContext);
    }

}

也许它有帮助