Mar*_*287 1 javascript safari ios
以下代码适用于所有浏览器,包括Mac上的Safari,但iPhone上的Safari除外.
我有一个可能正在运行的计时器对象,其定义如下:
//delay background change until animation is finished
lastTimer = setTimeout(function () {
$('#' + targetDiv).removeClass('open');
}, 150);
Run Code Online (Sandbox Code Playgroud)
稍后,我需要检查计时器是否正在运行,如果是,则取消它.这是我正在使用的代码:
if (lastTimer != null) { clearTimeout(lastTimer); }
Run Code Online (Sandbox Code Playgroud)
这是IOS Safari生成JavaScript错误的地方:
"ReferenceError:找不到变量:lastTimer".
关于为什么检查null的任何想法都没有阻止错误,就像其他浏览器似乎一样?
以下是回答以下答复的两个相关功能的完整代码:(使用解决方案编辑)
// Class for handling the animations for the drop down menus
var dropDownMenu = {
lastTimer: null,
openMenu: function (targetDiv) {
if (targetDiv != null) {
var targetHeight = $('#' + targetDiv).height();
$('#' + targetDiv).stop(true); //stop an previous animations and clear queue
if (this.lastTimer != null) { clearTimeout(this.lastTimer); } //stop possible pending timer to prevent background change
console.log("testing b");
$('#mainNavigation #dropDownMenu ul').removeClass('open'); // make sure all closed menus show corrent bgd
$('#' + targetDiv).animate({
bottom: -(targetHeight + 30)
}, 200, 'swing');
$('#' + targetDiv).addClass('open');
}
},
closeMenu: function (targetDiv) {
if (targetDiv != null) {
$('#' + targetDiv).stop(true); //stop an previous animations and clear queue
$('#' + targetDiv).animate({
bottom: 0
}, 200, 'swing');
//delay background change until animation is finished
this.lastTimer = setTimeout(function () {
$('#' + targetDiv).removeClass('open');
}, 150);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当ios中发生错误时,执行将停止,并且我的测试console.log不会立即执行.
我想在此解释一下.使用简单检查检查未定义时,Mobile Safari不太宽容,
if variable
Run Code Online (Sandbox Code Playgroud)
当你遇到这样的情况然后使用,
if typeof variable === "undefined"
Run Code Online (Sandbox Code Playgroud)
将变量附加到"this"是这里的一个解决方案,但它只是利用了defineVariable.undefinedProperty返回undefined的事实,而直接引用未定义的变量将导致某些运行时环境中的引用错误.
如果没有必要,我建议不要养成附加"这个"的习惯.
| 归档时间: |
|
| 查看次数: |
11767 次 |
| 最近记录: |