Home > Web Front-end > JS Tutorial > body text

Why is my `setTimeout` function call executing immediately?

Linda Hamilton
Release: 2024-11-15 13:25:03
Original
540 people have browsed it

Why is my `setTimeout` function call executing immediately?

Why is my function call scheduled by setTimeout executed immediately?

In an attempt to test proxy servers, a function titled crawl() has been devised. The objective is for this function to invoke doRequest() at intervals of roughly 10 seconds. However, despite employing setTimeout(), the function is being called immediately.

To remedy this, there are three viable solutions:

  • Alter the order of arguments:

    setTimeout(doRequest, proxytimeout, url, proxys[proxy]);
    Copy after login
  • Use an evaluable string:

    setTimeout('doRequest('+url+','+proxys[proxy]+')', proxytimeout);
    Copy after login
  • Pass an anonymous function:

    (function(u, p, t) {
      setTimeout(function() { doRequest(u, p); }, t);
    })(url, proxys[proxy], proxytimeout);
    Copy after login

The above is the detailed content of Why is my `setTimeout` function call executing immediately?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template