如何修复" Uncaught TypeError:无法设置' currentTime'属于' HTMLMediaElement'

时间:2016-07-15 18:57:12

标签: jquery arrays video parseint

我正在使用Cuepoint.js创建文本链接,将HTML5视频提示为与特定文本行对应的时间标记。我需要动态地将已经写入数组的时间值作为字符串分配给链接。我知道我需要使用parseInt()在从数组中检索值时将值重新整形为整数。由于链接及其时间是动态的,我还需要在for循环中将链接分配给它们,然后将它们推送到另一个数组。这是代码

    var cueTimes = [];
    for (var j = 0; j < textItems.length; j++) {
        var times = parseInt(timeItems[j]);
        cueTimes.push(times);
        $(".field-name-field-text-"+(j+1)+" > div > div").click(function(){ cuepoint.setTime(cueTimes[j])});
    console.log(cueTimes[j]);
    }

但是,当我运行代码时,我收到此错误。

未捕获的TypeError:无法设置&#39; currentTime&#39;属性&#39; HTMLMediaElement&#39;:提供的double值是非限制性的。

据我所知,整数不是由jQuery正确读取的。但是,当我将时间值输出到我的控制台时,它们显示为整数。当我使用静态数组索引测试代码时(例如设置cueTimes [0]),代码运行并按预期工作。

有关如何修复/解决此问题的任何建议都非常感激。

谢谢!

以下是Cuepoint.jS文件中提示错误的行:

Cuepoint.prototype.setTime = function(time) {
        this.time = time;
        this.video.currentTime = time;
        return this.video.play();
    };

Cuepoint.js代码:

(function() {
/* Cuepoint Coffee. A simple library for HTML5 Video Subtitles and Cuepoints */

/**
 * @class Utils 
*/

var Cuepoint, Utils, utils;
Utils = (function() {
    function Utils() {}
    Utils.prototype.log = function(args) {
        this.args = args;
        if (window.console) {
            return console.log(Array.prototype.slice.call(this, arguments));
        }
    };
    return Utils;
})();

/**
 * @class Cuepoint
 */

Cuepoint = (function() {
    function Cuepoint() {
        this.nativeKeys = Object.keys;
    }
    Cuepoint.prototype.init = function(slides) {
        var key, value, _results;
        this.slides = slides;
        this.subtitles = document.getElementById("subtitles");
        this.video = document.getElementById("video");
        _results = [];
        for (key in slides) {
            value = slides[key];
            this.addSlide(key, value);
            _results.push(this.events.call);
        }
        return _results;
    };
    Cuepoint.prototype.events = function() {};
    Cuepoint.prototype.currentTime = function() {
        return this.video.currentTime;
    };
    Cuepoint.prototype.update = function(html) {
        this.html = html;
        return this.subtitles.innerHTML = this.html;
    };
    Cuepoint.prototype.setTime = function(time) {
        this.time = time;
        this.video.currentTime = time;
        return this.video.play();
    };
    Cuepoint.prototype.addSlide = function(time, html) {
        var self;
        this.time = time;
        this.html = html;
        self = this;
        return this.video.addEventListener("timeupdate", function() {
            if (this.currentTime >= time && this.currentTime <= time + 0.3) {
                return self.update(html);
            }
        },
        false);
    };
    Cuepoint.prototype.play = function() {
        return this.video.play();
    };
    Cuepoint.prototype.pause = function() {
        if (!this.video.paused) {
            return this.video.pause();
        }
    };
    return Cuepoint;
})();
utils = new Utils;
window.cuepoint = new Cuepoint;

})调用(这);

1 个答案:

答案 0 :(得分:0)

我明白了。 click()函数的闭包导致迭代器变量的值在该函数中丢失。我通过创建一个自动执行函数来解决它,在本地访问变量,这里解释:

此代码完成了它:

for(var j = 0; j< textItems.length; j++){
            (function(k){
            text[k] = parseInt(timeItems[k]);
            $(".field-name-field-text-"+(k+1)+" > div > div").click(function(){ cuepoint.setTime(text[k])});
        })(j);  
        }