2017-08-11 35 views
2

工作,我需要點擊按鈕後只有5秒跑完的功能。的setTimeout不是我的點擊數事件

控制檯顯示此消息:

VM9200:1 Uncaught ReferenceError: getId is not defined at :1:1

function getId() { 
 
    $.getJSON("https://maxtechglobal.com/vencimientos/perfil/perfil.php?cuit="+$("#cuit").val(), function(result){ 
 
    for (var i = 0; i < result.data.length; i++) { 
 
     $.each(result.data[i].perfil, function(index, value) { 
 
     var id = result.data[i].perfil.id; 
 
     $("#idPerfil").val(id); 
 
     }); 
 
    } 
 
    }); 
 
}
<button id="btnBuscar" onclick="setTimeout('getId()', 5000);" type="button" class="btn btn-primary text-center" name="button">Buscar</button>

我證明了這一點:setTimeout(getId, 5000);setTimeout("getId();", 5000);這個:setTimeout('getId', 5000);

但任何工作

+5

當您使用'on *'事件屬性時,'getId()'需要在窗口範圍內聲明。我打賭你已經把它放在一個document.ready處理程序下。另外請注意,您可以通過將函數引用傳遞給超時來改進代碼:'setTimeout(getId,50000);' –

+0

該代碼應該工作減去沒有引用jQuery。我的猜測是,這不是你的實際代碼,你的實際代碼有一個文件準備包裝 – epascarello

回答

2

在這裏,你去了一個解決方案

$('#btnBuscar').click(function(){ 
 
    setTimeout(function(){ 
 
    $.getJSON("https://maxtechglobal.com/vencimientos/perfil/perfil.php?cuit="+$("#cuit").val(), function(result){ 
 
     for (var i = 0; i < result.data.length; i++) { 
 
     $.each(result.data[i].perfil, function(index, value) { 
 
      var id = result.data[i].perfil.id; 
 
      $("#idPerfil").val(id); 
 
     }); 
 
     } 
 
    }); 
 
    }, 5000); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button id="btnBuscar" type="submit" class="btn btn-primary text-center" name="button">Buscar</button>

既然你是jQuery,所以我用點擊事件jQuery

$('#btnBuscar').click(function(){}); 

我使用setTimeout內部click方法&加入的代碼內的線(而不是關閉的另一功能)。

+0

請添加你給的答案的描述。這不是我第一次看到你這樣做。回答一個答案不會教育任何人。 –

+0

@RoryMcCrossan謝謝。我補充說,但我正在考慮如何放置。 – Shiladitya

0

給你 - 工作示例

function getId() { 
 
    alert('function'); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button id="btnBuscar" onclick="setTimeout(getId, 3000);" type="button" class="btn btn-primary text-center" name="button">Buscar</button>

+0

不知道爲什麼這是downvoted - 它的作品 –

2

getId()需要當您使用on*事件屬性被下的窗口範圍內聲明。我打賭你已經把它放在一個document.ready處理程序下。

另外請注意,你可以提高代碼,並通過將功能參考到超時,並通過使用一個不顯眼的事件處理程序,而不是過時的on*屬性避免這個問題。嘗試:

function getId() { 
 
    console.log('called!'); 
 

 
    // your AJAX logic here... 
 
} 
 

 
$(function() { 
 
    $('#btnBuscar').click(function() { 
 
    setTimeout(getId, 5000); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button id="btnBuscar" type="button" class="btn btn-primary text-center" name="button">Buscar</button>

+1

迄今爲止最好的解決方案。 –

0

的setTimeout接受給函數作爲第一個參數的參考。 這可以是一個函數名:

function explode(){ 
    console.log('called!'); 
} 
setTimeout(explode, 5000); 

引用的功能的變量:

var explode = getId(){ 
    console.log('called!'); 
}; 
setTimeout(explode, 5000); 

或匿名功能:

setTimeout(getId(){ 
    console.log('called!'); 
}, 5000); 

也可以通過setTimeout的一串代碼爲它來執行:

setTimeout("console.log('called!');", 5000);