Javascript code

Does this code hav any wrong?

    setInterval(
      a = location.replace("https://exampl.example.repl.co");
      a.preventDefault();
      $.ajax({
        type:'POST',
        url:'/',
        //data:{
        //  todo:$("#todo").val()
        //},
        success:function(){
          var usetime = Date.now() - timeping;
          alert(usetime+"ms");
        }
      });
    ,1000);

Hi @louis. Thank you for your post.

In order for the community to suggest ideas to help you solve your code issue it would be useful to share a link to your Repl.

Any error messages that you see appear when you attempt to run your code would also be useful to share here.

1 Like

The first parameter of setInterval is a function, but you haven’t made a function. You also have a variable, a, which you don’t actually use.

Your code should look something like this:

setInterval(function() {
	// a isn't actually used here and so appears unnecessary
	a = location.replace("https://exampl.example.repl.co");
	a.preventDefault();
	// you could do
	// location.replace("https://exampl.example.repl.co").preventDefault();
	$.ajax({
		type: "POST",
		URL: "/",
		//data: {
			//todo:$("#todo").val()
		//},
		success: function() {
			// I'm assuming timeping is defined outside of this function, if not, this will produce an error
			alert((Date.now() - timeping) + "ms");
		}
	});
}, 1000);
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.