我正在尝试解决一个奇怪的JavaScript问题:这个脚本在所有浏览器中都工作得很好,但在IE中不起作用,只有在打开开发工具后才能正常运行。
我搜索了一下,发现这通常是由于IE没有生成控制台对象造成的。然而,在代码中没有提到控制台,并且我尝试了5种以上的不同代码来解决这个问题,但都没有成功。
希望能得到一些帮助,解决这个问题!
代码:
var slide = function slider() {
var i = 0;
var slider = {
loop: function loop(data) {
$.getJSON('?getdata=1', function(data) {
var create = $('<div class="social-area pic">' + '<div class="socimgdiv">' + '<img class="socimg" src="' + data.pic + '">' + '<div class="infotxt">' + data.name + '<br><small>' + data.age + ', ' + data.country + '</small><br>' + '</div></div></div>'),
maxTimeout = 4000,
minimumTimeout = 1000;
$('#box').prepend(create);
$('.social-area').last().fadeOut(400);
setTimeout(function() {
$('.social-area').last().remove();
}, 400);
setTimeout(function() {
$('.pic').animate({
width: 'toggle'
}, 350).removeClass('pic');
}, 400);
i += 1;
if (i >= 5) {
timeouter = Math.floor(Math.random() * maxTimeout);
if (timeouter <= minimumTimeout) timeouter = minimumTimeout;
} else timeouter = 400;
setTimeout(function() {
this.loop();
}.bind(slider), timeouter);
});
}
};
slider.loop();
};
slide();
该脚本从先前包含的PHP脚本生成的JSON源中收集数据。
if(isset($_GET['getdata'])){
echo json_encode(array(
'name' => $name,
'gender' => $gender,
'age' => $age,
'country' => $mycountry,
'pic' => $pic
)); exit;
}
这一切在FF、Chrome、Safari和Opera中都很好地运作 - 但是在未打开开发者工具的IE 11中却不行。
可能有一些调用或函数在生成控制台对象之前就无法正常工作,但我不知道是什么!
if (typeof console == 'undefined') {console = {}}; if (!console.log) {console.log = function (){}};
- slebetman