gpt4 book ai didi

javascript - 语法错误: identifier starts immediately after numeric literal - setTimeout or concatenation?

转载 作者:行者123 更新时间:2023-11-28 19:42:26 25 4
gpt4 key购买 nike

我的 JS 代码中的一行有问题:

window.setTimeout('window.location.href = "http://holy-war.net/town/alchemist/?w="' + world + ';',2000);

其中 world 是一个包含文本“4IN”(无引号)的字符串。也许我的串联在某种程度上被破坏了,或者您甚至无法将变量传递到 setTimeout 函数中?是哪个?

最佳答案

是的,您的串联已损坏。它将生成代码

window.location.href = "http://holy-war.net/town/alchemist/?w="4IN;
// ^^^

它准确地显示了错误消息所描述的问题。相反:

  • 修正报价

    window.setTimeout('window.location.href = "http://holy-war.net/town/alchemist/?w=' + world + '";', 2000);
  • 使用正确的序列化

    window.setTimeout('window.location.href = '+JSON.stringify('http://holy-war.net/town/alchemist/?w=' + world) + ';', 2000);
  • 根本不要生成代码来eval,按照您应该的方式使用函数!

    window.setTimeout(function() {
    window.location.href = 'http://holy-war.net/town/alchemist/?w=' + world;
    }, 2000);

关于javascript - 语法错误: identifier starts immediately after numeric literal - setTimeout or concatenation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24837989/

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