为什么我不能在浏览器或JS小提琴中运行我的代码?

时间:2016-08-11 19:17:41

标签: javascript html

通过全面披露,我对JS来说是全新的。我不完全理解语言的本质(例如,它是用HTML编写的,JS和HTML之间有什么区别?)。不用说,我是新手。话虽这么说,我正在尝试在jspsych中构建一个实验,我浏览了教程,尝试运行示例代码,并得到“无效的JavaScript代码”。我找不到任何答案,为什么会这样。它既不适用于浏览器,也不适用于JS Fiddle的测试软件。我很感激任何帮助。

P.S。我是一个体面的Python程序员,JS的语法看起来非常简单,但在格式化等方面我出海了。

if (window.JSZip && window.JSZip.support && window.JSZip.support.blob) {
    oldGenerate = window.JSZip.prototype.generate;
    oldJSZip = window.JSZip;
    window.JSZip.prototype.generate = function (options) {
        blobForSave = oldGenerate.call(JSZipInstance, _.extend(
            {},
            options,
            {
                type: 'blob',
                mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            }
        ));
        return '';
    };
    window.JSZip = function () {
        JSZipInstance = new oldJSZip();
        return JSZipInstance;
    };
    workbook.toDataURL();
    window.JSZip = oldJSZip;
    window.JSZip.prototype.generate = oldGenerate;

    saveAs(blobForSave, fileName);
} else {
    kendo.saveAs({
        dataURI: workbook.toDataURL(),
        fileName: fileName
    });
}

3 个答案:

答案 0 :(得分:0)

您是否更新了此处发布的代码?看起来你仍然有一些丢失的逗号,正如我在这里指出的那样" //< - 你需要一个COMMA在这里"。见下文:

    var welcome_block = {
        type: "text", //<-- YOU NEED A COMMA HERE
        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>", //<-- YOU NEED A COMMA HERE
        timing_post_trial: 2000
    };

编辑:另一个错字(math.random()应该是Math.random());

 var post_trial_gap = function() {
    return Math.floor( Math.random() * 1500) + 750;
}

答案 1 :(得分:0)

以下是更新后的代码:

/* 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();
    }
    });

答案 2 :(得分:0)

在获取库并创建蓝色和橙色png之后,我将实验进行了以下更正到您的上一个代码帖子。您拥有的插件名称不正确。顺便说一下,我的时间是303毫秒。

<script src="jspsych-5.0.3/plugins/jspsych-single-stim.js"></script>