定义了一个全局变量,但Chrome控制台返回undefined

时间:2017-05-23 17:42:08

标签: javascript jquery variables

这是我的代码:

 ArrayList<Object> myArrayList = new ArrayList<Object>();
 ArrayList<Integer> list1 = new ArrayList<Integer>;

 myArrayList.add(element1);
 myArrayList.add(list1);

我声明了两个变量(一个对象和一个数字)。对象ArrayList<ArrayList<Integer>> myArrayList = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> list1 = new ArrayList<Integer>; ArrayList<Integer> list2 = new ArrayList<Integer>; list1.add(element1); list2.add(element2); list2.add(element3); myArrayList.add(list1); myArrayList.add(list2); 被正确定义,但当我尝试调用jQuery(function ($){ var player = $('#player'); var time = 0; function setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = "expires="+ d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; } player.bind('pause', function(){ console.log('player was paused'); console.log(player.currentTime); setCookie("time", player.currentTime, 2) }); }); 时,控制台返回undefined。

2 个答案:

答案 0 :(得分:0)

您尚未声明全局变量。这两个值在全局范围内都是未定义的。要拥有全局范围,您可以这样定义。

=IF(F1="",SUMIF(B:B,B2,F:F),"")

答案 1 :(得分:-1)

你真的可以将它定义为全局吗?

jQuery(function ($){
var player = $('#player');
window.time = 0;

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    }

player.bind('pause', function(){
    console.log('player was paused');
    console.log(player.currentTime);//what should curentTime be?
    setCookie("time", player.currentTime, 2);
    window.time=player.currentTime;//an example
});
});

现在它真的是全球性的......

//in the console
console.log(time);