error in the setTimeout function returned Uncaught SyntaxError: Unexpected identifier

error in the setTimeout function returned Uncaught SyntaxError: Unexpected identifier

我正在使用 Prestashop 1.6,路径中有一个 javascript 文件,theme/mytheme/js/autoload/shopflores.js 其中 returns 是一个错误。代码:

setTimeout(
  $.ajax({
    type: 'get',
    url: urlformChx,
    async: true,
    cache: false,
    data: {
      ajax: true,
      token: token,
      idcart: idCart,
      method: 'getCurrentShop'
    },
    success: function(resultx) {
      console.log(resultx);
      obj = JSON.parse(resultx);
      r = obj.region;

      if (obj.code == '200' && r != null) {
        $('#order-opc #uniform-regshopflores > span').text(obj.region);
        $("#regshopflores option").each(function() {
          if ($(this).text() == obj.region) {
            $(this).attr('selected', true);
          }
        });

        $('#order-opc .delivery_option_radio').parent('span').removeClass('checked');
        $('#order-opc .delivery_option_radio').removeAttr("checked");

        $('#uniform-seltiendaflores > span').text(obj.tienda);
        $("#seltiendaflores option").text(obj.tienda);

        $.each($("input.delivery_option_radio"), function() {
          //console.log($(this)); 
          var idx = obj.carrier + ',';
          if ($(this).val() == idx) {
            console.log(idx);
            $(this).parent('span').addClass('checked');
            $(this).attr('checked', 'checked');
          }
        });
      }
    }
  }), 5000);

在 Google Chrome 控制台中进行测试 returns 此错误消息:

VM667:1 Uncaught SyntaxError: Unexpected identifier
setTimeout (async)
(anonymous) @ shopflores.js:113
dispatch @ jquery-1.11.0.min.js:3
r.handle @ jquery-1.11.0.min.js:3
trigger @ jquery-1.11.0.min.js:3
e.event.trigger @ jquery-migrate-1.2.1.min.js:2
(anonymous) @ jquery-1.11.0.min.js:3
each @ jquery-1.11.0.min.js:2
each @ jquery-1.11.0.min.js:2
trigger @ jquery-1.11.0.min.js:3
ready @ jquery-1.11.0.min.js:2
K @ jquery-1.11.0.min.js:2

非常感谢您的帮助!

setTimeout() 需要一个函数,但您向它提供了 $.ajax() 调用的结果,它是一个 jqXHR 对象。因此,当 JS 尝试将对象作为函数调用时,您会收到错误消息。

要解决此问题,请将您的 AJAX 逻辑放在您作为参数提供给 setTimeout() 的匿名函数中:

setTimeout(function() {
  $.ajax({
    // your AJAX call here...
  })
}, 5000);