简体   繁体   中英

Javascript function does not execute, script continues normally

It's been a long night trying to solve this one.

I'm trying to load a small text file, parse it, then use the information to provide the user with video options. I do this when the page loads but I also do it in response to a user event. In both cases I get the same result. The load_playList function does not execute.

The code is below. The window.load and selectVideo(X) routines are the starting points. In both cases the load_Playlist function is ignored.

It seems that load_playList never executes. The alert message is never executed, yet the script continues as if everything were normal. It's as if I typed the function's name wrong, so I did that and the script failed. So, the browser seems to see the function, but ignores it.

var videoList = [];
var videoTitles = [];
var videoCaptions = [];

/*
var videoList = [
    'videos/ZionParkParade.mp4', 
    'videos/Pointless2014.mp4'];
var videoTitles = [
    'The 50GT Zion Canyon Cruise',
    'The Tinyvette at Sonoma Raceway'];
var videoCaptions = ['Caption 1','Caption 2'];
*/

window.onload = function()
{
    alert(0);
    load_playList;      // Loads and parses a small text file.
    alert(1);
    load_video(0);      // Set up the first video to play.
    alert(2);
}

function load_playList()
{
    alert('load_playList');
    var listFile = ReadFile('videos/PlayList.txt');

    var playList = listFile.split('\n');

    var j = 0;
  for (i = 0; i < math.trunc(playList.length / 3); i++) 
    {
        videoList[i] = playList[j];
        videoTitles[i] = playList[j+1];
        videoCaptions[i] = playList[j+2];
        j++;
        j++;
        j++
    }
}

function selectVideo(X)
{
    alert(10);
    load_playList;      // Loads and parses a small text file.
    alert(11);
    load_video(Number(X));
    alert(12);
}

function FileRead(U) 
{
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        X=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        X=new ActiveXObject("Microsoft.XMLHTTP");
    }

  X.open('GET', U, false );
  X.setRequestHeader('Content-Type', 'text/html')
  X.send();
    return X.responseText;
}   

function load_video(N)
{
    var V = document.getElementById("video_player");
    V.pause();
    V.src = videoList[N];
    V.auto = false;
    V.type = "video/mp4";

    // Update the title and captions.
  document.getElementById('pause_button').innerHTML = "Play";
    document.getElementById('videoTitle').innerHTML = videoTitles[N];
    document.getElementById('videoCaption').innerHTML = videoCaptions[N];
}

If I un-comment the initial variable declarations, o provide initial values, everything works, except the text file is never loaded.

Thanks in advance.

Edit - I found two problems in the load_playList routine but still can't get that function to run. I don't even see the first alert.

I pasted the load_playList code into the onload routine and it works. I can live with that, but danged if it makes any sense.

window.onload = function()
{
//  load_playList;      // Loads and parses a small text file.

    var listFile = load_file('videos/PlayList.txt');
    var playList = listFile.split('\n');

    var j = 0;
  for (i = 0; i < Math.trunc(playList.length / 3); i++) 
    {
        videoList[i] = playList[j];
        videoTitles[i] = playList[j+1];
        videoCaptions[i] = playList[j+2];
        j++;
        j++;
        j++
    }

    load_video(0);      // Set up the first video to play.
    var vid = document.getElementById("video_player");
    vid.volume = 0.2;   
}

function load_playList()
{
alert(10);
    var listFile = load_file('videos/PlayList.txt');
alert(11);
    var playList = listFile.split('\n');

alert('Length = '+playList.length); 
alert('Count = '+Math.trunc(playList.length / 3));  

    var j = 0;
  for (i = 0; i < Math.trunc(playList.length / 3); i++) 
    {
        videoList[i] = playList[j];
        videoTitles[i] = playList[j+1];
        videoCaptions[i] = playList[j+2];
        j++;
        j++;
        j++
    }
alert(12);
}

Just include () after the function name. Just calling the name won't run the function.

load_playList();

Refer Also refer the fiddle for watching a function call

The work-around, pasting that routine's code into the onload routine, worked, but I wasn't satisfied and finally stumbled on this as a solution:

window.onload = function()
{
    load_playList(0);       // Loads and parses a small text file.
    load_dropdown(0);       // Populate the dropdown menu.
    load_video(0);          // Set up the first video to play.
    var vid = document.getElementById("video_player");
    vid.volume = 0.2;   
}

Neither load_playList nor load_dropdown need an argument passed to them but I did anyway, and that worked.

I'm not sure why this is so but I'll take it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM