【问题标题】:Smart GWT JSNI executes only onceSmart GWT JSNI 只执行一次
【发布时间】:2015-07-20 07:01:50
【问题描述】:

我对 Smart GWT 应用程序中的 JSNI 执行有疑问。

我想实现用户活动监控功能。当用户处于非活动状态 n 分钟时,页面将触发通知模式并调用用户返回。类似于 9gag 上的唤醒通知。

在通知被触发后,我将实现一些额外的功能(例如,计算用户处于非活动状态的次数......)

问题: 正常工作的 JSNI 函数(我在纯 HTML 和 JavaScript 页面上测试过)只执行一次。将第一个不活动通知模式推送给用户后,功能不再执行。

public static native void wakeUp()/*-{
            console.log("#### Wake Up - started");
            var timeout;
            resetTimer();

            document.onmousemove = resetTimer;
            document.onclick = resetTimer;

            function resetTimer() {
                clearTimeout(timeout);
                timeout = setTimeout(function() {
                    console.log("#### Wake Up - inactive for 1 minute...");
                    //display custom modal
                    @com.pb.client.MGMWeb::displayWakeUpNotification()();
                }, 1 * 10 * 1000);//inactivity duration
            }
}-*/;

【问题讨论】:

    标签: javascript gwt smartgwt jsni


    【解决方案1】:

    解决方案是将事件侦听器添加到 $doc 而不是使用文档。

    工作示例:

    public static native void wakeUp()/*-{
        console.log("#### WakeUp - Start");
        var timeout;
        resetTimer();
    
        $doc.addEventListener('onclick', resetTimer, false);
        $doc.addEventListener('onmouseover', resetTimer, false);
        $doc.addEventListener('mousedown', resetTimer, false);
        $doc.addEventListener('keypress', resetTimer, false);
        $doc.addEventListener('mousemove', resetTimer, false);
        $doc.addEventListener("DOMMouseScroll", resetTimer, false);
        $doc.addEventListener("MSPointerMove", resetTimer, false);
        $doc.addEventListener("touchmove", resetTimer, false);
    
        function notifiyUser() {
            @com.pb.client.MGMWeb::displayWakeUpNotification()();
            console.log("#### Wake Up - inactive for 1 minute..");
        }
    
        function resetTimer() {
            clearTimeout(timeout);
            timeout = setTimeout(notifiyUser, 1 * 60 * 1000);
        }
    }-*/;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多