gpt4 book ai didi

javascript - JSHint 和 JSLint 未捕获未定义的函数

转载 作者:行者123 更新时间:2023-12-02 16:47:03 24 4
gpt4 key购买 nike

最近,我一直在使用 JavaScript 为我的网站开发 cookie 系统。现在,当我将它们放在一起时,它们拒绝在 Chrome 的 JavaScript 控制台中运行。

它说TypeError: undefined is not a function尽管一切似乎都定义正确。

我已经通过 JSLint 和 JSHint 运行了这个,他们没有发现任何错误。

    //Date finder
var day = new Date();
day = day.substring(4, 100);

//Pause function for later use
function sleep(ms) {
var currentTime = new Date().getTime();
while (currentTime + ms >= new Date().getTime()) {
alert("Please be patient while we connect...");
}
}

//Sets cookie with cookie name, cookie value, and cookie expiry (converted into milliseconds by function)
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;
}

//Uses a while loop to search through cookies outputed by "document.cookie" and find desired cookie
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1);
}
if (c.indexOf(name) != -1) {
return c.substring(name.length, c.length);
}
}
return "";
}

//Option to delete cookies if client desires
function deleteCookie(del) {
if (del == "Y") {
var checkDel = confirm("Are you sure? Your username will be lost.");
if (checkDel === false) {
document.cookie = "username=; day";
}
}
}

//User input area - either uses cookie to identify user or requests user to input value
function checkCookie() {
var user = getCookie("username");
if (user !== "") {
alert("Welcome again " + user);
var clearCookie = prompt("Do you wish to remove your cookie from this site?", "Y or N").toUpperCase();
deleteCookie(clearCookie);
} else {
user = prompt("Please enter your name:").toUpperCase(0,1);
if (user !== "" && user !== null) {
setCookie("username", user, 91);
}
}
}
alert("Please wait while we connect you to our servers.\nPress ENTER to initiate.");
console.log("Logging onto system...");
sleep(3800);
console.log("User request: " + Math.floor(Math.random()*10000) + "; Server received request; Connection accepted; " + day);
checkCookie();

最佳答案

错误发生在这里:

var day = new Date();
day = day.substring(4, 100);

JavaScript 中的日期存储为公开许多函数的对象,substring 不是其中之一。

您可以在日期对象上使用 toString() 方法,如下所示:

day.toString().substring(4, 100);

或者您可以使用 getDay() 方法返回日期实例工作日的索引:

['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'][day.getDay()-1];

关于javascript - JSHint 和 JSLint 未捕获未定义的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27030913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com