如何延迟变量和函数?

时间:2016-04-12 12:16:39

标签: javascript jquery firebase document-ready

我的控制台出现错误,其中显示" Firebase未定义"。

1。看起来像这样" base"代码避免在另一个之前加载。

EDIT无法将脚本放入标题中,因此需要这样做

var script   = document.createElement("script");
script.type  = "text/javascript";
script.src   = "https://cdn.firebase.com/v0/firebase.js";
document.head.appendChild(script);

2。所以我需要以某种方式推迟这些。我将如何实现这一目标?

var myDataRef = new Firebase('https://eloquatest.firebaseio.com/');
myDataRef.on('child_added', function(snapshot) {
var post = snapshot.val();
});

function submitPost(e) {
var myDataRef = new Firebase('https://eloquatest.firebaseio.com/');
var name = $('#field0').val();
myDataRef.push({name: name});
$('#field0').val('');
}

第3。我试图把它放在$( document ).ready(function()内但是没有用。 链接:https://learn.jquery.com/using-jquery-core/document-ready/

2 个答案:

答案 0 :(得分:1)

以这种方式使用var s = Snap("#svg-snap .svg-design-i"); var line = s.select("#se-line"); var box = line.getBBox(); var cx = box.width/2+box.x; var cy = box.height/2+box.y; line.animate({ transform: "r15," + cx + ',' + cy}, 1000, mina.easeIn, function() { line.animate({ transform: "r0," + cx + ',' + cy}, 1000, mina.easeIn, function() { animSnap(); }) }); 的{​​{1}}方法:

onload

答案 1 :(得分:1)

您要找的是onload事件:

var script = document.createElement("script");
document.head.appendChild(script);

script.onload = function() {
    var myDataRef = new Firebase('https://eloquatest.firebaseio.com/');
    myDataRef.on('child_added', function(snapshot) {
        var post = snapshot.val();
    });

    function submitPost(e) {
        var myDataRef = new Firebase('https://eloquatest.firebaseio.com/');
        var name = $('#field0').val();
        myDataRef.push({name: name});
        $('#field0').val('');
    }
};
script.type = "text/javascript";
script.src = "https://cdn.firebase.com/v0/firebase.js";

请注意,根据此SO帖子[Trying to fire the onload event on script tag],这些操作(调用appendChild,设置srconload)的顺序很重要。避风港检查一下。

此外,由于您使用的是jQuery,您可能需要查看getScript函数:http://api.jquery.com/jQuery.getScript/