简体   繁体   English

Javascript函数第二次执行

[英]Javascript functions executes on second time

I am using the jQuery webcam plugin to capture an image from the webcam. 我正在使用jQuery网络摄像头插件从网络摄像头捕获图像。 I have all script files in order. 我按顺序准备了所有脚本文件。 This button is used to make the capture() call to get the webcam's snapshot: 此按钮用于进行capture()调用以获取网络摄像头的快照:

<input id="capture" type="submit" value="Capture my shot!" onclick="webcam.capture();" />

The webcam is called through the following function: 通过以下功能调用网络摄像头:

$("#cam-space").webcam({
    width: 560,
    height: 420,
    mode: "save",
    swffile: "javascript/jscam.swf",
    onTick: function (remain) {
        if (0 == remain) {
            jQuery("#timer-status").text("Cheese!");
        } else {
            jQuery("#timer-status").text(remain + " seconds remaining...");
        }
    },
    onSave: function () {
    },
    onCapture: function () {
        webcam.save("/capture/image");
    },
    debug: function () { },
    onLoad: function () { }
});

My problem is that when I clicked the button the first time, I'm getting the following error in Chrome's debugger: 我的问题是,当我第一次单击该按钮时,我在Chrome的调试器中收到以下错误:

Uncaught TypeError: undefined is not a function (onclick)

But when I click it the second time, it works and takes the snapshot. 但是,当我第二次单击它时,它可以工作并拍摄快照。 What may be the problems? 可能是什么问题?

The function webcam.capture() won't be defined until the Webcam plugin finishes loading. 在网络摄像头插件加载完成之前,不会定义功能webcam.capture() Instead of using an onclick attribute on the button, you should bind the event listener after the plugin is loaded: 代替在按钮上使用onclick属性,您应该在插件加载后绑定事件侦听器:

$("#cam-space").webcam({
    /* ... */
    onLoad: function() {
        $("#capture").on("click", webcam.capture);
    }
    /* ... */
});

Additionally, as Mritunjay suggested in the comments , make sure your $("#cam-space").webcam() statement is contained in $(document).ready(function() { }); 此外,如Mritunjay 在评论中所建议 ,请确保$("#cam-space").webcam()语句包含在$(document).ready(function() { }); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript Second for循环永远不会执行 - Javascript Second for loop never executes 我的代码第一次正确执行,但是第二次却没有正确执行(Javascript)。 似乎变量未更新正确 - My code executes correctly the first time, but not the second (Javascript). It seems that a variable is not updated correclty 第二次单击后立即执行Javascript - Javascript executes just after second click Javascript函数仅在第二次悬停后执行 - Javascript function executes only after second hover setInterval仅在间隔结束时执行函数 - setInterval executes functions only by the time of the interval ends Javascript 函数仅每隔一次执行一次 - Javascript function only executes every other time JavaScript将无法再次运行 - JavaScript will not run a second time 第二次调用promise在promise返回之前执行 - Angular Javascript - Second call to promise executes before the promise returns - Angular Javascript Javascript此关键字在原型函数中递归执行一次,然后未定义 - Javascript this keyword in recursion in prototype functions executes once and then undefined 执行两个onsubmit函数; 仅当第一个函数为true时,第二个函数才执行 - Execute two onsubmit functions; second function executes only if first function is true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM