[英]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.