奇怪的IE,Javascript仅适用于开发模式(F12)

栏目: JavaScript · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/31107364/weird-ie-javascript-only-works-in-development-mode-f12

基本上,我有两个标签,一个是隐藏的,一个是显示的.

隐藏的一个将在后台加载视频,当前一个完成时,我将通过隐藏显示的一个来交换两个.

我还将在服务器端列出可播放的视频列表,我将使用ajax获取列表并决定下一个要加载的列表.

无论如何,你很难尝试这个,因为你需要一个视频服务器,我发现很难砍掉代码,所以我只会向你展示整个源代码(带一些注释),希望你能理解.

我使用了jquery和video.js.源代码如下,

HTML:

<div class="container" id="video-container-1">
</div>
<div class="container" id="video-container-2">
</div>

使用Javascript:

//I am making a live video by chopping the video into MP4 files of 800ms each
//The play rate has to be adjustable or the local browser and live server will not sync
var play_rate = { 1.0:800, 1.01:790, 1.02:785, 1.03:777, 1.04:770, 1.05:762, 1.06:755, 1.07:748,
  1.08:741, 1.09:734, 1.1:727 };
var min_rate=1.0;
var max_rate=1.1;
var base_rate = 1.03;
var current_rate = base_rate;
var timer_value = play_rate[current_rate];

var key_to_play;
var timer_id;
var newest_key;
var video_server_address = "192.168.100.1:20001";


var current_play = 1;
var container = new Array();
var player = new Array;

function video_html(container_id, id) {
  return '<video id="video-js-' + container_id + '" class="video-js vjs-default-skin" ' +
  ' preload="auto" width="960" height="540"  crossorigin="anonymous" ' +
  'data-setup=\'{"example_option":true}\'>' +
  '\t<source src="http://' + video_server_address +'/live/' + id + '.mp4" type="video/mp4" /> \n' +
  '\t\t<track id="video-vtt" kind="subtitle" label="english" srclang="en" src="http://' + video_server_address + '/live/' + id + '.vtt" default></track>\n ' +
  '\t\t<p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>\n' +
  '</video>';
}
function play_next()
{
  var next_play;
  if (current_play == 1)
    next_play = 2;
  else
    next_play = 1;

  player[next_play - 1].play();
  container[next_play - 1].show();
  container[current_play - 1].hide();
  if (player[current_play - 1])
    player[current_play - 1].dispose();

  key_to_play++;

  //switch current & next
  current_play = next_play;

  timer_id = setTimeout(function() {
    play_next();
  }, timer_value);

  //Assuming get list + load video < 700ms
  $.get("http://" + video_server_address + "/live/list", 
    function(list){
      keys = list["keys"];
      newest_key = keys[keys.length - 1];


      console.log("key_to_play: " + key_to_play + "  newest_key: " + newest_key);

      var next_play;
      if (current_play == 1)
        next_play = 2;
      else
        next_play = 1;
      //-----------------begin-------------------
      //not really useful to you because these are just 
      //to let the video play sync with video server, we can safely 
      //remove these but the video play will out of sync after some time    
      if (key_to_play > newest_key)
      {
        //too fast
        //make it slower?
        if (current_rate > min_rate)
        {
          current_rate = current_rate - 0.01;
          timer_value = play_rate[current_rate];
        }
        else
        {
          //it is already 1.0 (the slowest settings)
          //have to crop on the timer_value
          timer_value = play_rate[current_rate] + 5 * (key_to_play - newest_key);
        }
        //either wait or play again? just play again and test for stability first
        key_to_play = newest_key;
      }
      else if (key_to_play == newest_key || key_to_play == newest_key - 1)
      {
        //the best situation we got
        //don't change anything
      }
      else
      {
        //a little slow
        if (current_rate < max_rate)
        {
          current_rate = current_rate + 0.01;
          timer_value = play_rate[current_rate];
        }
        else
        {
          timer_value = play_rate[current_rate] - 5 * (newest_key - key_to_play);
        }

        //tooo slow, drop 4 * 800ms data
        if (newest_key - key_to_play > 5)
        {
          key_to_play = newest_key - 1;
        }
      }
      //-------------------end------------

      container[next_play - 1].html(video_html(next_play, key_to_play));
      player[next_play - 1] = videojs('video-js-' + next_play, {}, function(){
        // Player (this) is initialized and ready.
        //the following is only to make the browser show subtitle
        //without this, the FF will not show subtitle by default
        $('#video-container-' + next_play + " div.vjs-subtitles-button li.vjs-menu-item").eq(1).trigger('click');
      });
      player[next_play - 1].playbackRate(current_rate);



      console.log(timer_value);
    }
  );



}

$( document ).ready(function() {
  $.get("http://" + video_server_address + "/live/list", function(list){
    keys = list["keys"];
    key_to_play = keys[keys.length - 2];
    newest_key = keys[keys.length - 1];

    container[0] = $("div#video-container-1");
    container[1] = $("div#video-container-2");
    container[0].hide();

    container[1].html(video_html(2, key_to_play));
    player[0] = null;
    player[1] = videojs("video-js-2",{}, function(){
      // Player (this) is initialized and ready.
      console.log($("#video-container-2 div.vjs-subtitles-button li.vjs-menu-item").eq(1).text());
      $("#video-container-2 div.vjs-subtitles-button li.vjs-menu-item").eq(1).trigger('click');
    });
    player[1].playbackRate(current_rate);

    play_next();
  });
});
</script>

这段代码在chrome和FF上工作得很好,但是,当尝试使用IE11时,新视频将无法加载,它将每800毫秒在两个视频(比如视频1和2)之间切换,我认为它将加载后者(3) ,4,5等一个),但它不会播放,它只是继续播放1和2以及1和2.

当我尝试调试它时,我打开IE11开发工具,然后当开发 工具 准备就绪时,IE11将运行良好.

只要我关闭开发工具,IE11就会很糟糕.

我想也许IE11做了一些优化并优化了一些代码?我怎么检查?

谢谢.

取自对原始问题的评论:

Why does JavaScript only work after opening developer tools in IE once?

对于OP来说,这是他的Ajax请求的缓存导致了这个问题.通过禁用缓存解决:

$.ajax({cache: false, ...})

翻译自:https://stackoverflow.com/questions/31107364/weird-ie-javascript-only-works-in-development-mode-f12


以上所述就是小编给大家介绍的《奇怪的IE,Javascript仅适用于开发模式(F12)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

当下的启蒙

当下的启蒙

[美] 史迪芬·平克 / 侯新智、欧阳明亮、魏薇 / 浙江人民出版社 / 2018-12 / 159.90

[编辑推荐] ● 比尔•盖茨最喜爱的一本书。理查德·道金斯心中的诺贝尔文学奖作品。尤瓦尔•赫拉利2018年最爱的书之一。 ● 当代最伟大思想家史蒂芬·平克全面超越自我的巅峰之作,一部关于人类进步的英雄史诗。 ●《当下的启蒙》用数据和事实揭示出世界的真相:不是黑暗,而是光明;不是丧,而是燃;我们没有退步,而是一直在进步,还将继续进步。用这本书点燃生活的勇气,亲手创造更美好的未来。 ......一起来看看 《当下的启蒙》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具