setTimeout不适用于safari mobile [英] setTimeout not working on safari mobile
问题描述
我有一个功能,点击它时会显示一个菜单,我希望它在5秒后消失。这是我的javascript - 它可以在桌面浏览器上正常工作,但它不会在移动设备上消失。
I have a function that shows a menu when clicking on it, and I want it to disappear after 5 seconds. This is my javascript - it works properly on desktop browser but it doesn't disappear on the mobile ones.
$(function() {
$('#prod_btn').click(function() {
$(this).addClass('selected').next('ul').css('display', 'block');
setTimeout(hideMenu, 5000);
});
});
function hideMenu() {
$('#prod_btn').removeClass('selected').next('ul').css('display', 'none');
}
问题出在哪里?
谢谢
推荐答案
我遇到了同样的问题。我的代码在我的Mac上的任何浏览器中都运行良好,但在iOs设备上它不起作用。
I've just had the same problem. My code is running great in any browser on my Mac, but on iOs devices it doesn't work.
我在超时功能上使用.bind(this)这就是导致我这个问题的原因。
当我在我的脚本中使用.bind扩展函数对象时,它就像一个魅力。
I use ".bind(this)" on my timeout function and that is what is causing the problem for me. When I extend the function object with ".bind" in my script it works like a charm.
我的代码是这样的:
searchTimeout = setTimeout(function() {
...
}.bind(this),250);
为了在iOs设备上工作,我(如上所述)刚添加了这个:
For this to work on iOs devices I (like mentioned above) just added this:
Function.prototype.bind = function(parent) {
var f = this;
var args = [];
for (var a = 1; a < arguments.length; a++) {
args[args.length] = arguments[a];
}
var temp = function() {
return f.apply(parent, args);
}
return(temp);
}
我没有在你的setTimeout上看到任何.bind,但对于其他人来说同样的问题可能是这个问题。这就是我发帖的原因: - )
I don't see any .bind on your setTimeout, but for others with the same problem this may be the issue. That's why I'm posting :-)
这篇关于setTimeout不适用于safari mobile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!