Javascript 为什么可以';我不能在浏览器或JS Fiddle中运行代码吗?
充分披露后,我对JS是全新的。我不完全理解这种语言的本质(例如,它是用html编写的,JS和html之间有什么区别?)。所以不用说,我是个新手。话虽如此,我正在尝试在jspsych中构建一个实验,我阅读了教程,尝试运行示例代码,得到了“无效的JavaScript代码”。我找不到任何答案来解释为什么会这样。它既不适用于浏览器,也不适用于JS Fiddle的测试软件。我将非常感谢您的帮助 顺便说一句,我是一个正派的Python程序员,JS语法看起来很直截了当,但在格式化等方面,我却一窍不通Javascript 为什么可以';我不能在浏览器或JS Fiddle中运行代码吗?,javascript,html,Javascript,Html,充分披露后,我对JS是全新的。我不完全理解这种语言的本质(例如,它是用html编写的,JS和html之间有什么区别?)。所以不用说,我是个新手。话虽如此,我正在尝试在jspsych中构建一个实验,我阅读了教程,尝试运行示例代码,得到了“无效的JavaScript代码”。我找不到任何答案来解释为什么会这样。它既不适用于浏览器,也不适用于JS Fiddle的测试软件。我将非常感谢您的帮助 顺便说一句,我是一个正派的Python程序员,JS语法看起来很直截了当,但在格式化等方面,我却一窍不通 <
<html>
<head>
<title>My experiment</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="jspsych-5.0.3/jspsych.js"></script>
<script src="jspsych-5.0.3/plugins/jspsych-text.js"></script>
<script src="jspsych-5.0.3/plugins/jspysch-single-stim.js"></script>
<link href="jspsych-5.0.3/css/jspsych.css" rel="stylesheet" type="text/css"></link>
</head>
<body>
</body>
<script>
/* define welcome message block */
var welcome_block = {
type: "text"
text: "Welcome to the experiment. Press any key to begin."
};
/*define instructions block*/
var instructions_block = {
type:"text",
text:"<p>In this experiment,a circle will appear in the center" +
"of the screen.</p><p>If the circle is <strong>blue</strong>," +
"press the letter F on the keyboard as fast as you can.</p>" +
"<p>If the circle is <strong>orange</strong>, do not press " +
"any key.</p>"+
"<div class ='left center-conten'><img src='img/blue.png'></img>" +
"<p class = 'small'><strong>Press the F key</strong></p></div>" +
"div class='right center-content'><img src='img/orang.png'></img>" +
"<p class='small'><strong>Do not press a key</strong></p></div>" +
"<p>Press any key to begin.</p>"
timing_post_trial: 2000
};
var test_stimuli = [
{
stimulus: 'img/blue.png',
data: { response: 'go'}
},
{
stimulus: 'img/orange.png',
data: { response: 'no-go'}
}
];
var all_trials = jsPsych.randomization.repeat(test_stimuli,10);
var post_trial_gap = function() {
return Math.floor( math.random() * 1500) + 750;
}
var test_block = {
type: 'single-stim',
choices: ['F'],
timing_response: 1500,
timing_post_trial: post_trial_gap,
on_finish: function(data){
var correct = false;
if(data.response == 'go' && data.rt > -1){
correct = true;
} else if(data.response == 'no-go' && data.rt == -1){
correct = true;
}
jsPsych.data.addDataToLastTrial({correct: correct});
},
timeline: all_trials
};
/*define debrief block*/
function getSubjectData() {
var trials = jsPsych.data.getTrialsofType('single-stim');
var sum_rt = 0;
var correct_trial_count = 0;
var correct_rt_count = 0;
for (var i = 0; i < trials.length; i++) {
if (trials[i].correct == true) {
correct_trial_count++;
if(trials[i].rt > -1){
sum_rt += trials[i].rt;
correct_rt_count++;
}
}
}
return {
rt: Math.floor(sum_rt/correct_rt_count),
accuracy: Math.floor(correct_trial_count / trials.length * 100)
}
}
var debrief_block = {
type: "text",
text: function() {
var subject_data = getSubjectData();
return"<p>You responded correctly on "+subject_data.accuracy+"% of "+
"the trials.</p><p>Your average response time was <strong>"+
subject_data.rt + "ms</strong>. Press any key to complete the "+
"experiment. Thank you!</p>";
}
};
/*create experiment timeline array*/
var timeline = [];
timeline.push(welcome_block);
timeline.push(instructions_block);
timeline.push(test_block);
timeline.pysh(debrief_block)
/*start the experiment*/
jsPsych.init({
experiment_structure: experiment,
on_finish: function() {
jsPsych.data.displayData();
}
});
</script>
</html>
我的实验
/*定义欢迎消息块*/
var欢迎\u块={
键入:“文本”
文字:“欢迎参加实验。按任意键开始。”
};
/*定义指令块*/
变量指令\u块={
键入:“文本”,
文本:“在本实验中,中心将出现一个圆”+
“如果圆圈是蓝色的,,”+
“尽可能快地按键盘上的字母F。”+
如果圆圈是橙色的,不要按+
“任意键。”+
"" +
“按F键
”+
“div class='right center'>”+
“请勿按键
”+
“按任意键开始。”
审判后的时间:2000年
};
变量测试\刺激=[
{
刺激:“img/blue.png”,
数据:{响应:'go'}
},
{
刺激:“img/orange.png”,
数据:{响应:'禁止'}
}
];
var all_试验=jsPsych.随机化.重复(试验刺激,10);
var post_试验_间隙=函数(){
返回Math.floor(Math.random()*1500)+750;
}
var测试块={
类型:“单刺激”,
选项:['F'],
响应时间:1500,
试验后的时间安排:试验后的间隙,
完成时:函数(数据){
var correct=false;
如果(data.response=='go'&&data.rt>-1){
正确=正确;
}else if(data.response=='no go'&&data.rt==-1){
正确=正确;
}
jsPsych.data.addDataToLastTrial({correct:correct});
},
时间表:所有审判
};
/*定义汇报块*/
函数getSubjectData(){
var-trials=jsPsych.data.getTrialsofType('single-stim');
var和=0;
var校正试验计数=0;
var正确计数=0;
对于(变量i=0;i-1){
求和rt+=试验[i].rt;
正确的计数++;
}
}
}
返回{
rt:数学地板(总和/正确计数),
准确度:数学地板(正确的试验次数/试验长度*100)
}
}
var debrief_块={
键入:“文本”,
文本:函数(){
var subject_data=getSubjectData();
返回“您正确回答了”+主题\数据。准确率+%of”+
“试验。您的平均反应时间”+
subject_data.rt+“ms。按任意键完成”+
“实验。谢谢!”;
}
};
/*创建实验时间线数组*/
var时间线=[];
时间轴推送(欢迎块);
时间轴推送(指令块);
时间轴推送(测试块);
timeline.pysh(汇报块)
/*开始实验*/
jsPsych.init({
实验结构:实验,
on_finish:function(){
jsPsych.data.displayData();
}
});
您是否更新了发布在此处的代码?看起来您仍然缺少一些逗号,正如我在这里用“//指出的,这是更新的代码:
/* define welcome message block */
var welcome_block = {
type: "text",
text: "Welcome to the experiment. Press any key to begin."
};
/* define instructions block */
var instructions_block = {
type: "text",
text: "<p>In this experiment, a circle will appear in the center " +
"of the screen.</p><p>If the circle is <strong>blue</strong>, " +
"press the letter F on the keyboard as fast as you can.</p>" +
"<p>If the circle is <strong>orange</strong>, do not press " +
"any key.</p>" +
"<div class='left center-content'><img src='img/blue.png'></img>" +
"<p class='small'><strong>Press the F key</strong></p></div>" +
"<div class='right center-content'><img src='img/orange.png'></img>" +
"<p class='small'><strong>Do not press a key</strong></p></div>" +
"<p>Press any key to begin.</p>",
timing_post_trial: 2000
};
/* define test block */
var test_stimuli = [
{
stimulus: "img/blue.png",
data: { response: 'go' }
},
{
stimulus: "img/orange.png",
data: { response: 'no-go' }
}
];
var all_trials = jsPsych.randomization.repeat(test_stimuli, 10);
var post_trial_gap = function() {
return Math.floor( Math.random() * 1500 ) + 750;
}
var test_block = {
type: "single-stim",
choices: ['F'],
timing_response: 1500,
timing_post_trial: post_trial_gap,
on_finish: function(data){
var correct = false;
if(data.response == 'go' && data.rt > -1){
correct = true;
} else if(data.response == 'no-go' && data.rt == -1){
correct = true;
}
jsPsych.data.addDataToLastTrial({correct: correct});
},
timeline: all_trials
};
/* define debrief block */
function getSubjectData() {
var trials = jsPsych.data.getTrialsOfType('single-stim');
var sum_rt = 0;
var correct_trial_count = 0;
var correct_rt_count = 0;
for (var i = 0; i < trials.length; i++) {
if (trials[i].correct == true) {
correct_trial_count++;
if(trials[i].rt > -1){
sum_rt += trials[i].rt;
correct_rt_count++;
}
}
}
return {
rt: Math.floor(sum_rt / correct_rt_count),
accuracy: Math.floor(correct_trial_count / trials.length * 100)
}
}
var debrief_block = {
type: "text",
text: function() {
var subject_data = getSubjectData();
return "<p>You responded correctly on "+subject_data.accuracy+"% of "+
"the trials.</p><p>Your average response time was <strong>" +
subject_data.rt + "ms</strong>. Press any key to complete the "+
"experiment. Thank you!</p>";
}
};
/* create experiment timeline array */
var timeline = [];
timeline.push(welcome_block);
timeline.push(instructions_block);
timeline.push(test_block);
timeline.push(debrief_block);
/* start the experiment */
jsPsych.init({
timeline: timeline,
on_finish: function() {
jsPsych.data.displayData();
}
});
/*定义欢迎消息块*/
var欢迎\u块={
键入:“文本”,
文字:“欢迎参加实验。按任意键开始。”
};
/*定义指令块*/
变量指令\u块={
键入:“文本”,
文本:“在本实验中,中心将出现一个圆”+
“如果圆圈是蓝色的,,”+
“尽可能快地按键盘上的字母F。”+
如果圆圈是橙色的,不要按+
“任意键。”+
"" +
“按F键
”+
"" +
“请勿按键
”+
“按任意键开始。”,
审判后的时间:2000年
};
/*定义测试块*/
变量测试\刺激=[
{
刺激:“img/blue.png”,
数据:{响应:'go'}
},
{
刺激:“img/orange.png”,
数据:{响应:'禁止'}
}
];
var all_试验=jsPsych.随机化.重复(试验刺激,10);
var post_试验_间隙=函数(){
返回Math.floor(Math.random()*1500)+750;
}
var测试块={
类型:“单刺激”,
选项:['F'],
响应时间:1500,
试验后的时间安排:试验后的间隙,
完成时:函数(数据){
var correct=false;
如果(data.response=='go'&&data.rt>-1){
正确=正确;
}else if(data.response=='no go'&&data.rt==-1){
正确=正确;
}
jsPsych.data.addDataToLastTrial({correct:correct});
},
时间表:所有审判
};
/*定义汇报块*/
函数getSubjectData(){
var-trials=jsPsych.data.getTrialsOfType('single-stim');
var和=0;
var corr
var post_trial_gap = function() {
return Math.floor( Math.random() * 1500) + 750;
}
/* define welcome message block */
var welcome_block = {
type: "text",
text: "Welcome to the experiment. Press any key to begin."
};
/* define instructions block */
var instructions_block = {
type: "text",
text: "<p>In this experiment, a circle will appear in the center " +
"of the screen.</p><p>If the circle is <strong>blue</strong>, " +
"press the letter F on the keyboard as fast as you can.</p>" +
"<p>If the circle is <strong>orange</strong>, do not press " +
"any key.</p>" +
"<div class='left center-content'><img src='img/blue.png'></img>" +
"<p class='small'><strong>Press the F key</strong></p></div>" +
"<div class='right center-content'><img src='img/orange.png'></img>" +
"<p class='small'><strong>Do not press a key</strong></p></div>" +
"<p>Press any key to begin.</p>",
timing_post_trial: 2000
};
/* define test block */
var test_stimuli = [
{
stimulus: "img/blue.png",
data: { response: 'go' }
},
{
stimulus: "img/orange.png",
data: { response: 'no-go' }
}
];
var all_trials = jsPsych.randomization.repeat(test_stimuli, 10);
var post_trial_gap = function() {
return Math.floor( Math.random() * 1500 ) + 750;
}
var test_block = {
type: "single-stim",
choices: ['F'],
timing_response: 1500,
timing_post_trial: post_trial_gap,
on_finish: function(data){
var correct = false;
if(data.response == 'go' && data.rt > -1){
correct = true;
} else if(data.response == 'no-go' && data.rt == -1){
correct = true;
}
jsPsych.data.addDataToLastTrial({correct: correct});
},
timeline: all_trials
};
/* define debrief block */
function getSubjectData() {
var trials = jsPsych.data.getTrialsOfType('single-stim');
var sum_rt = 0;
var correct_trial_count = 0;
var correct_rt_count = 0;
for (var i = 0; i < trials.length; i++) {
if (trials[i].correct == true) {
correct_trial_count++;
if(trials[i].rt > -1){
sum_rt += trials[i].rt;
correct_rt_count++;
}
}
}
return {
rt: Math.floor(sum_rt / correct_rt_count),
accuracy: Math.floor(correct_trial_count / trials.length * 100)
}
}
var debrief_block = {
type: "text",
text: function() {
var subject_data = getSubjectData();
return "<p>You responded correctly on "+subject_data.accuracy+"% of "+
"the trials.</p><p>Your average response time was <strong>" +
subject_data.rt + "ms</strong>. Press any key to complete the "+
"experiment. Thank you!</p>";
}
};
/* create experiment timeline array */
var timeline = [];
timeline.push(welcome_block);
timeline.push(instructions_block);
timeline.push(test_block);
timeline.push(debrief_block);
/* start the experiment */
jsPsych.init({
timeline: timeline,
on_finish: function() {
jsPsych.data.displayData();
}
});
<script src="jspsych-5.0.3/plugins/jspsych-single-stim.js"></script>