如何在js interval timeout上设置消息

时间:2015-08-02 02:44:31

标签: javascript

我有一个php标头和一个js函数,如果他们不移动鼠标10分钟就会将人们记录下来,我希望它在剩下1分钟时提醒他们但是无法让它工作。以下是有效的代码。

onload位于标题中。

import Foundation
import UIKit

class LabelWithAdaptiveTextHeight: UILabel {

override func layoutSubviews() {
    super.layoutSubviews()
    font = fontToFitHeight()
}

// Returns an UIFont that fits the new label's height.
private func fontToFitHeight() -> UIFont {

    var minFontSize: CGFloat = DISPLAY_FONT_MINIMUM // CGFloat 18
    var maxFontSize: CGFloat = DISPLAY_FONT_BIG     // CGFloat 67
    var fontSizeAverage: CGFloat = 0
    var textAndLabelHeightDiff: CGFloat = 0

    while (minFontSize <= maxFontSize) {
        fontSizeAverage = minFontSize + (maxFontSize - minFontSize) / 2

        if let labelText: NSString = text {
            let labelHeight = frame.size.height

            let testStringHeight = labelText.sizeWithAttributes(
                [NSFontAttributeName: font.fontWithSize(fontSizeAverage)]
            ).height

            textAndLabelHeightDiff = labelHeight - testStringHeight

            if (fontSizeAverage == minFontSize || fontSizeAverage == maxFontSize) {
                if (textAndLabelHeightDiff < 0) {
                    return font.fontWithSize(fontSizeAverage - 1)
                }
                return font.fontWithSize(fontSizeAverage)
            }

            if (textAndLabelHeightDiff < 0) {
                maxFontSize = fontSizeAverage - 1

            } else if (textAndLabelHeightDiff > 0) {
                minFontSize = fontSizeAverage + 1

            } else {
                return font.fontWithSize(fontSizeAverage)
            }
        }
    }
    return font.fontWithSize(fontSizeAverage)
}
}

然后是js

<body onload="set_interval()"
onmousemove="reset_interval()"
onclick="reset_interval()">

如果我遗漏了

,一切都很顺利
var timer = 0;
function set_interval() {
timer = setInterval("auto_logout()" { alert('You are being logged out, check if you have an inactive page') }, 60000);
  // milliseconds
}

function reset_interval() {
if (timer != 0) {
clearInterval(timer);
timer = 0;
timer = setInterval("auto_logout()" { alert('You are being logged out, check if you have an inactive page') }, 60000);
}
}

function auto_logout() {
window.location = "logout.php";
}

所以我知道问题存在,但不确定如何解决,请协助。即使它确实有效,它会在间隔结束时提醒他们(我认为),而我希望他们先获得警报,以便他们可以移动他们的鼠标。

2 个答案:

答案 0 :(得分:3)

var timer;
function set_interval() {
    timer = setInterval(auto_logout, 60000);
}

function reset_interval() {
    clearInterval(timer);
    set_interval();
}

function auto_logout() {
    alert('You are being logged out, check if you have an inactive page');
    window.location = "logout.php";
}

答案 1 :(得分:2)

我的建议是改为使用setTimeout()

&#13;
&#13;
var track;

var msg = function() {
  alert("You will be logged out in one minute !");
};

var logout = function() {
  alert("You have been logged out!");
  location.replace('logout.php');
}

var trace = function() {
  console.log('trace ...');
  track = setTimeout(logout, 1000 * 60);
};

var retrace = function() {
  console.log('retrace ...');
  clearTimeout(track);
  trace();
};
&#13;
html,
body {
  width: 100%;
  height: 100%;
  background: #ccc;
  border: 1px solid #aaa;
}
&#13;
<body onload="msg();trace()" onmousemove="retrace()" onclick="retrace();">Move your mouse here ...</body>
&#13;
&#13;
&#13;